UrlEncode for GridHyperLinkColumn??

7 posts, 0 answers
  1. Justin Jones
    Justin Jones avatar
    12 posts
    Member since:
    Jun 2008

    Posted 21 Jul 2008 Link to this post

    Hi there,

    I would like escape ampersands in the field that gets passed to  GridHyperLinkColumn..... What's the best way to do that?

    Currently I format the field like this... and am getting bad URLs when a field value has ampersands in it. 

            <telerik:GridHyperLinkColumn 
                HeaderText="Sector" SortExpression="Sector"
                UniqueName="Sector" DataNavigateUrlFormatString="Page.aspx?Sector={0}" 
                DataNavigateUrlFields="Sector" DataTextField="Sector" 
                FooterText="TOTAL:" FooterStyle-Font-Bold="true">
            <HeaderStyle Width="150px" />
            </telerik:GridHyperLinkColumn>

    Thx!
    Justin

  2. Dimo
    Admin
    Dimo avatar
    8458 posts

    Posted 23 Jul 2008 Link to this post

    Hi Justin,

    In order to change the HyperLinkColumn's URLs, you can subscribe to the ItemDataBound event. For example:

    ASPX

    <telerik:RadGrid 
        ID="RadGrid3" 
        runat="server" 
        OnItemDataBound="RadGrid3_ItemDataBound"

    C#

    protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e) 
        if (e.Item is GridDataItem) 
        { 
            (((e.Item as GridDataItem)["ColumnUniqueName"as TableCell).Controls[0] as HyperLink).NavigateUrl = (((e.Item as GridDataItem)["ColumnUniqueName"as TableCell).Controls[0] as HyperLink).NavigateUrl.Replace("&""&amp;"); 
        } 
    }                                                                                                                                                                                            



    Greetings,
    Dimo
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 Jul 2008 Link to this post

    Hi Justin,

    You can also refer the following help article which explains how to access cells and rows in RadGrid.
    Accessing cells and rows

    Thanks
    Shinu.
  4. Justin Jones
    Justin Jones avatar
    12 posts
    Member since:
    Jun 2008

    Posted 28 Jul 2008 Link to this post

    Cool thanks guys... Getting the hang of it now... : )
  5. Barnabas
    Barnabas avatar
    18 posts
    Member since:
    Jul 2009

    Posted 23 Oct 2009 Link to this post

    I have an addendum to this question. I am doing a similar thing, but with a twist. Here are my modifications:

    DataNavigateUrlFormatString="Page.aspx?Id={1}&Sector={0}"  

    DataNavigateUrlFields="Id,Sector"

    Id and Sector can each have a variety of characters that need to be Url Encoded. Including & : # % * and potentially others. So, I would like to make a call to HttpUtility.UrlEncode on the actual data fields before they are constructed into a url for the link. How would I go about doing this?
  6. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 26 Oct 2009 Link to this post

    Hello Barnabas,

    You myay consider executing the UrlEncode method inside the ItemDataBound handler of the grid getting access to the actual hyperlink as explained by my colleague Dimo in his post.

    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Barnabas
    Barnabas avatar
    18 posts
    Member since:
    Jul 2009

    Posted 29 Oct 2009 Link to this post

    I figured out what I needed to do. I thought I would share for posterity. (And myself, so when I forget what I did, I can find it here.)

    To clarify, this is the code that I had is above. Here is the url that it was producing:
    Page.aspx?Id=0:#0&AIN&Sector=19823 
    There are only two query string parameters there. But the new page wasn't able to parse them because of the ampersands and hash signs. (Update: You can see by the syntax highlighting that this isn't working correctly. The solution presented in this thread didn't work for me because I need to UrlEncode more that just ampersands. In addition I didn't want the ampersands that separated parameters UrlEncoded.)

    The first part of my solution was to move Id parameter to be in the last position yielding
    DataNavigateUrlFormatString="Page.aspx?Sector={0}&Id={1}"   
    DataNavigateUrlFields="Sector,Id" 

    Then I parsed the string to pull out the value of the last parameter and did a UrlEncode on that.
    var url = control.NavigateUrl; 
    var idStart = url.IndexOf("&id=") + 4; 
    string id = url.Substring(idStart); 
    string urlEncodedId = HttpUtility.UrlEncode(id); 
    string newUrl = url.Replace(id, urlEncodedId); 
    control.NavigateUrl = newUrl; 


    That did the trick. If you have do to this for more than one field its gonna get tougher
Back to Top