This is a migrated thread and some comments may be shown as answers.
UrlEncode for GridHyperLinkColumn??
6 Answers 167 Views
This is a migrated thread and some comments may be shown as answers.
Justin Jones
Top achievements
Rank 1
Justin Jones asked on 21 Jul 2008, 05:20 PM
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

6 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 23 Jul 2008, 11:41 AM
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
0
Shinu
Top achievements
Rank 1
answered on 24 Jul 2008, 03:48 AM
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.
0
Justin Jones
Top achievements
Rank 1
answered on 28 Jul 2008, 09:02 PM
Cool thanks guys... Getting the hang of it now... : )
0
Barnabas
Top achievements
Rank 1
answered on 23 Oct 2009, 04:59 PM
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?
0
Sebastian
Telerik team
answered on 26 Oct 2009, 11:20 AM
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.
0
Barnabas
Top achievements
Rank 1
answered on 29 Oct 2009, 07:57 PM
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
Tags
Grid
Asked by
Justin Jones
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Shinu
Top achievements
Rank 1
Justin Jones
Top achievements
Rank 1
Barnabas
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or