hguser hguser - 1 year ago 120
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

I meet some problem:

This is the code :

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

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

For example,the
of one row maybe


Now the generated url would be :


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

I tried this:


It does not work also.

Any ideas?

Answer Source

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


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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download