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

Editing GridHyperLinkColumn programmatically

4 Answers 235 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 27 Mar 2009, 02:50 PM

Please forgive me if this question is elemental - I am new to ASP.NET, Telerik, and C#, so I'm struggling a bit.  Still, I have managed to create a project that uses Forms Authentication and take me to the page containing my first grid, among other things.  I just need another light bulb to go off, then I'll be on my way :)

My RadGrid is named RadGrid1.  It contains the following GridHyperLInkColumn.

 

 

<telerik:GridHyperLinkColumn

 

 

    HeaderText="eMail"

 

 

    Text="email"

 

 

    UniqueName="email_link"

 

 

    SortExpression="email"

 

 

    HeaderStyle-Width="25px"

 

 

    DataNavigateUrlFields="email"

 

 

    DataNavigateUrlFormatString="mailto:{0}"

 

 

    HeaderStyle-HorizontalAlign="Center"

 

 

    ItemStyle-HorizontalAlign="Center">

 

 

    <HeaderStyle HorizontalAlign="Center" />

 

 

    <ItemStyle HorizontalAlign="Center" />

 

 

</telerik:GridHyperLinkColumn>

As the grid is being prepared, I want to read the value of the email column in my data source, and if is is null I want to modify the hyperlink column so that the text '(none)' is displayed and the 'mailto:{0}' in the DataNavigateUrlFormatString = ''.  In other words, I only want to display a valid hyperlink if there's an email for this row.

If someone could show me exactly what to do - every loop and line - it would help immensely.

Thanks in advance!
S--

 

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 27 Mar 2009, 06:15 PM
Hello Steven,

Please test the following code-snippet:
<telerik:GridTemplateColumn HeaderText="eMail" UniqueName="email_link" SortExpression="email" 
    HeaderStyle-Width="25px"
    <HeaderStyle HorizontalAlign="Center" /> 
    <ItemStyle HorizontalAlign="Center" /> 
    <ItemTemplate> 
        <asp:HyperLink ID="HyperLink1" NavigateUrl='<%# String.Format("mailto:{0}", Eval("Name").ToString()) %>' runat="server" 
            Text="email" Visible='<%# String.IsNullOrEmpty(Eval("Name").ToString()) %>'></asp:HyperLink> 
    </ItemTemplate> 
</telerik:GridTemplateColumn> 

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Steven
Top achievements
Rank 1
answered on 27 Mar 2009, 06:31 PM
Thanks, Daniel, works perfectly - altho I had to reverse the evaluation of email to make it do what I wanted - 

!String.IsNullOrEmpty(Eval("email").ToString()).
But is there a way to add 'IF' logic to the eval statement so that I can assign one of two possible values to the 'Text' property, as in ..... if IsNullOrEmpty.... Text = '(none)'.... Else Text = 'eMail' ?  Then I could leave the value visible at all times while still preventing any action against a missing email address.
Thanks!

0
Todd Anglin
Top achievements
Rank 2
answered on 27 Mar 2009, 07:04 PM
Steven-

You should be able to use code like this:

NavigateUrl='<%# (String.IsNullOrEmpty(Eval("Name").ToString())) ? String.Format("mailto:{0}", Eval("Name").ToString()) : "None" %>'

This is called a Ternary Operator and it makes it easy to "If True Then X Else Y" in a single line. Hope that helps!

-Todd

UPDATE: Reverse my logic. :) (String.IsNullOrEmpty(...)) ? "None" : "mailto:"
0
Steven
Top achievements
Rank 1
answered on 27 Mar 2009, 07:09 PM

Thanks, Todd, that's just the sort of thing I was hoping to learn.  The light bulbs are going off at an alarming rate!  I will work with this.

Steve--

Tags
Grid
Asked by
Steven
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Steven
Top achievements
Rank 1
Todd Anglin
Top achievements
Rank 2
Share this question
or