hguser hguser - 3 months ago 25
ASP.NET (C#) Question

the DataNavigateUrlFormatString escape problem in asp.net's HyperLinkField

I am usin the HyperLinkField in my asp.net application.

However when I use the

DateNavigateUrlFormation
I meet some problem:

This is the code :

<asp:hyperlinkfield datanavigateurlfields="tab_name,rowid"
DataNavigateUrlFormatString="~\details.aspx?tab={0}&rowid={1}" />


Since sometime the
rowid
may contain some characters which has the specified meaning in the http,so in the server side I can not get the correct
rowid
.

For example,the
rowid
of one row maybe

AAAAAAAXXX+BA


Now the generated url would be :

http://xxx/details.aspx?tab=tab_name&rowid=AAAAAAAXXX+BA

Since there is a character
+
here,I want to excape it.

I tried this:

DataNavigateUrlFormatString="~\details.aspx?tab={0}&rowid=<%#Server.HtmlEncode({1})%>"


It does not work also.

Any ideas?

Answer

You need to handle this in the RowDataBound event.

if (e.Row.RowType == DataControlRowType.DataRow)
{ 
    e.Row.Cells[indexOfYourColumn].Text = Server.HtmlEncode(e.Row.Cells[indexOfYourColumn].Text);
}

Update

Take a look at this article

According to that you can programatically access the hyperlink field.

HyperLinkField hLink = GridView1.Columns[0] as HyperLinkField;
hLink.DataNavigateUrlFormatString = "details.aspx?pi=" + GridView1.PageIndex.ToString() + "&eID={0}";

OR I think what you need is to handle it in the RowDataBound event.

HyperLink hLink = e.Row.Cells[0].Controls[0] as HyperLink;
hLink.NavigateUrl = "test.aspx?q=" + e.Row.Cells[0].Text;