Reputation: 36058
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?
Upvotes: 0
Views: 2507
Reputation: 444
This aswer is based on Johan van der Slikke's one. I just added Uri.EscapeDataString:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" Text="Link" NavigateUrl='<%# String.Format("~/details.aspx?tab={0}&rowid={1}", Uri.EscapeDataString(Convert.ToString(Eval("tab_name"))), Uri.EscapeDataString(Convert.ToString(Eval("rowid")))) %>' />
</ItemTemplate>
</asp:TemplateField>
Upvotes: 2
Reputation: 765
Use a TemplateField which does support the databinding.
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" Text="Link" NavigateUrl='<%# String.Format("~/details.aspx?tab={0}&rowid={1}", Convert.ToString(Eval("tab_name")), Convert.ToString(Eval("rowid"))) %>' />
</ItemTemplate>
</asp:TemplateField>
This way you don't need code in CodeBehind and you don't need to access columns by index (which can easily change and thus lead to problems in the future).
Upvotes: 3
Reputation: 19872
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
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;
Upvotes: 0