I am in the process of building a content management style website for my friend who writes articles on a regular basis. I was handling section navigation with QueryStrings because I didn't know anything about URL rewriting, and it was quick and easy. It was working fine until he added a section with an apostrophe in it. This actually throws an error on ASP.NET. Server.URLEncode apparently ignores apostrophes. But even when I manually replaced the ' with %27 (the hex value) it still threw an error. However, two apostrophes in the URL will give the desired behavior.
A simple way I went about solving this was with .Replace("'","''") to replace a single apostrophe with two of them.
So, in my case I was using a datalist to display the sections and just tacked that on to the URL:
<asp:DataList ID="dlSections" runat="server" DataSourceID="SqlSection">
<ItemTemplate>
- <a href="section.aspx?section=<%# Eval("section").Replace("'","''") %>" ><%# Eval("section")%></a>
</ItemTemplate>
</asp:DataList>
I also used the QueryString to display the current section, during the PageLoad in the codebehind. It would work the same from there, except now replacing the twin apostrophes with a single one:
lblSection.Text = Request.QueryString("section").Replace("''", "'")
Hope this helps someone.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5