Search

Categories

Fourth Dimension

<<  July 2008  >>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Blogroll

Tags

Recent posts

Recent comments

Apostrophes and QueryStrings in ASP.NET

Posted on January 31, 2008 02:53 by ruisu

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

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 8. 2008 13:00