Jay
Jay

Reputation: 27492

in hyperlinkfield, how to query string escape values?

I have a Hyperlinkfield in a GridView that has DataNavigateUrlFormatString="blahblah/dostuff.aspx?id={0}". But then I hit a case where the field substituted for the {0} has a plus sign in it. It is not escaped for a query string, so now it looks like a space instead of a plus.

I assume similar issues would arise if it would include ampersands, equal-signs or percent-signs.

Q: Is there any way to do a query string escape on the parameter?

I guess I could turn it into a TemplateField and an Eval. At best that's messy and ugly. This seems like an issue that would come up all the time. Is there a clean way to do the escape?

Upvotes: 1

Views: 308

Answers (1)

Stilgar
Stilgar

Reputation: 23571

After a bit of searching I have come to the conclusion that there is no easy solution to this. You can use Eval with no template field to generate the URL directly. Some suggest that you handle the RowDataBound event and find the right column and fix the URL but this seems like extremely ugly solution to me.

The cleanest solution which requires a lot of work is to create a class with the sole purpose to feed the GridView (you can even put it directly in the code behind) and encode the properties in C# code in the code behind. This is like creating a ViewModel in MVC just to feed a specific view.

Upvotes: 1

Related Questions