Editing GridHyperLinkColumn programmatically

5 posts, 0 answers
  1. Steven
    Steven avatar
    3 posts
    Member since:
    Feb 2009

    Posted 27 Mar 2009 Link to this post

    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--

     

  2. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 27 Mar 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Steven
    Steven avatar
    3 posts
    Member since:
    Feb 2009

    Posted 27 Mar 2009 Link to this post

    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!

  5. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 27 Mar 2009 Link to this post

    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:"
  6. Steven
    Steven avatar
    3 posts
    Member since:
    Feb 2009

    Posted 27 Mar 2009 Link to this post

    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--

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017