In my C# Web Application, I have a GridView with a TemplatField. Within the TemplateField is a HyperLink control. My goal is to bind this HyperLink control using Custom Expression Binding. I want the NavigateUrl to point to:
Visit.aspx?name=XXX
XXX in the link above is the database column name. My first instinct is to use the Custom Expression Binding method called Eval() to bind this column. An example of the use of this semi-working method below should be included in the <asp:HyperLink> control.
<asp:HyperLink NavigateUrl=’<%# “Visit.aspx?name=” + Eval(“name”) %>’></asp:HyperLink>
I say this semi-works because the evaulted “name” must not contain any special characters like the ampersand (&). Then the next idea is trying to encode the Eval(“name”). This however was not entirely clear. After researching, I found the following solution.
<asp:HyperLink NavigateUrl=’<%# “Visit.aspx?name=” + HttpUtility.UrlEncode(Eval(“name”).ToString()) %>’></asp:HyperLink>
And this is how to properly encode a hyperlink url that is bound using Custom Expression Binding. Though this may not be visible from the browser, the web page source will reveal that the ampersand (&) has been replaced with %26, which is its equivalent.
The last step on the following page my require decoding the encoded url.
RSS Feed
Posted in
Tags: 
