This is a migrated thread and some comments may be shown as answers.

GridHyperLinkColumn - Conditional Linking

8 Answers 347 Views
Grid
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 21 Oct 2010, 01:48 AM
I want to be able to list organizations whether or not they have websites. If they have a website, I want the GridHyperLinkColumn text property = WEB and it would be the hyperlink to link to the organization's website. If they don't have a website, then I want the text property = "" or nothing.

Well, if you don't do anything, you get WEB in the website column whether or not they have a website - now the WEB for live hyperlinks are underlined and work whereas the others are just text with no underline - so it looks bad.

So I thought I would try to use some sort of conditional statement on the Itembound subroutine. I use a class named "Company" and "CheckForWeb" is an instantiation of that class. "GetCompany" is a function in the Company class that will get the company record from the database with the "CompanyID" (id) in the RadGrid row. Here is what I did:

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim id As String = item.GetDataKeyValue("CompanyID").ToString
  
            Dim CheckForWeb As Company
            CheckForWeb = Company.GetCompany(CInt(id))
        ' see if the object is populated
            If CheckForWeb.CompanyID.HasValue Then
        ' check to see if there is a valid URL from the datafield WebsiteURL
                If CheckForWeb.WebsiteURL.Length > 10 Then
                    item("CompanyWebsite").Text = "WEB"
                Else
                    item("CompanyWebsite").Text = ""
                End If
            End If
        End If
    End Sub

Now, that resulted in companies that have websites having WEB in the website column and nothing if they didn't have a website BUT the link was not live - it was just text.

So after scouring the forum, I decided to add a couple of lines to the code to add a hyperlink as shown below.

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim id As String = item.GetDataKeyValue("CompanyID").ToString
 
            Dim CheckForWeb As Company
            CheckForWeb = Company.GetCompany(CInt(id))
        ' see if the object is populated
            If CheckForWeb.CompanyID.HasValue Then
        ' check to see if there is a valid URL from the datafield WebsiteURL
                If CheckForWeb.WebsiteURL.Length > 10 Then
                    item("CompanyWebsite").Text = "WEB"
                     Dim link As HyperLink = DirectCast(item("CompanyWebsite").Controls(0), HyperLink)
                    link.NavigateUrl = CheckForWeb.WebsiteURL
                Else
                    item("CompanyWebsite").Text = ""
                End If
            End If
        End If
    End Sub

That was really no good as I got the following alert error: Specified argument was out of the range of valid values. Parameter name: index - so I'm back to square one. Any help would be appreciated.

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Oct 2010, 10:58 AM
Hello William,

You need to access the HyperLink control first and then set the Text like below.

VB.Net:
If companyWeb.Length > 0 Then
    HprLnk = DirectCast(item("CompanyWebsite").Controls(0), HyperLink)
    HprLnk.NavigateUrl = "http://www.google.com"
    HprLnk.Text = "Web"
Else
    HprLnk.Text = " "
End If

Thanks,
Princy.
0
William
Top achievements
Rank 1
answered on 21 Oct 2010, 01:31 PM
Thank you for your reply. I tried  what you said - basically moving the text setting line to the last - and all I get is WEB as the text but it is not a live hyperlink - just text. I even tried hardcoding a link like you did for the NavigateURL and it still was just text in the cell. Again, thanks for answering my post so quickly - very much appreciated.

William
0
Accepted
Dimo
Telerik team
answered on 25 Oct 2010, 04:41 PM
Hi William,

Please inspect the following demo, which works, and compare with your implementation.

<%@ Page Language="VB" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<script runat="server">
 
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs)
        If TypeOf e.Item Is GridDataItem Then
            Dim hl As HyperLink = DirectCast(DirectCast(e.Item, GridDataItem)("WebCol").Controls(0), HyperLink)
            If String.IsNullOrEmpty(hl.NavigateUrl) Then
                hl.Visible = False
                DirectCast(hl.Parent, TableCell).Text = "&nbsp;"
            End If
        End If
    End Sub
     
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
<head runat="server">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>RadControls</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
 
<telerik:RadGrid
    ID="RadGrid1"
    runat="server"
    Width="400px"
    AutoGenerateColumns="false"
    DataSourceID="XmlDataSource1" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView>
        <Columns>
            <telerik:GridHyperLinkColumn HeaderText="Website" UniqueName="WebCol"
                DataNavigateUrlFields="CompanyUrl" DataTextField="CompanyName" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 
<asp:XmlDataSource ID="XmlDataSource1" runat="server">
    <Data>
        <nodes>
            <node ID="1" CompanyName="Company 1" CompanyUrl="http://www.company1.com" />
            <node ID="2" CompanyName="Company 2" CompanyUrl="http://www.company2.com" />
            <node ID="3" CompanyName="Company 3" CompanyUrl="" />
            <node ID="4" CompanyName="Company 4" CompanyUrl="" />
        </nodes>
    </Data>
</asp:XmlDataSource>
 
</form>
</body>
</html>


Sincerely yours,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
William
Top achievements
Rank 1
answered on 26 Oct 2010, 02:21 PM
Your example does work if I want to use DataTextFields and show CompanyName. What I want is for the cell to say "WEB" or something of my choice (maybe even an image) and be a live link - if there is no website, then there would be nothing in the column. The CompanyName is already on the grid so I don't want to repeat it.

I'm thinking about adding another field called "WebsiteExists" to my table and if the website does exist, then the field value will be "WEB" (without the quotation marks) - if not, then nothing will be there. That way, I can use "WebsiteExists" as my DataTextField.

I was wondering if an "IIF" statement on the .aspx page could do the trick - i.e. conditionally Eval a data field and then put the word "WEB" in there.

Ought to be an easier way though - Many thanks for your response.
0
Accepted
Dimo
Telerik team
answered on 26 Oct 2010, 03:07 PM
Hi William,

Well, yes, there is an easier way:

<telerik:GridHyperLinkColumn HeaderText="Website" UniqueName="WebCol" DataNavigateUrlFields="CompanyUrl" DataTextField="CompanyName" DataTextFormatString="WEB" />

Regards,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
William
Top achievements
Rank 1
answered on 26 Oct 2010, 03:22 PM
Well, I have tried that using your code behind and DataTextFormatString="WEB" and "WEB" shows up whether there is a URL or not - it IS live if there is a URL and it is just text if there isn't a URL. Thank you for your reply!
0
William
Top achievements
Rank 1
answered on 26 Oct 2010, 03:30 PM
OK, it worked - sort of surprised but I'll take it. Is there anyway to put an image in there rather than verbiage?
0
Dimo
Telerik team
answered on 27 Oct 2010, 07:59 AM
Hi William,

You can put an image programmatically in ItemDataBound, or add an <img> tag in the DataTextFormatString.

Greetings,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
William
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or