Results 1 to 5 of 5

Thread: Is there a way to clear the QueryString before PostBack?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved Is there a way to clear the QueryString before PostBack?

    I have a somewhat difficult problem...

    The project I have is complicated so I made a simpler example to explain.

    I have WebForm1.aspx with a Hyperlink control (Hyperlink1) and a Button control (Button1)

    The Hyperlink NavigateURL is WebForm1.aspx?id=0

    and the Page_Load and Button1_Click events are:
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    	if(Request.QueryString["id"] != null)
    	{
    		Response.Write(Request.QueryString["id"].ToString() + "<BR>");
    	}
    	else // do something else
    	{
    		Response.Write("id is null<BR>");
    	}
    }
    
    private void Button1_Click(object sender, System.EventArgs e)
    {
    	Page.RegisterClientScriptBlock("", "<script>alert('Button1 Click');</script>");
    	Response.Redirect("WebForm1.aspx");
    }
    Now, if I click the Hyperlink, I'm redirected back to the same page and the QueryString is shown in the Address Bar - http://localhost/QueryString/WebForm1.aspx?id=0

    and 0 is output at the top of the page.

    Next, I want to click Button1 but when the page reposts I want the QueryString variable "id" to be null or just be something different. The only way I could find to clear it is to just Redirect back to the page itself.

    The problem with that is, the code before the redirect won't show up on the page and any code after the redirect won't execute at all. If I comment out the Redirect line, the code executes, but the QueryString variable is still set.

    Last edited by wey97; Nov 10th, 2004 at 09:38 AM.

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Try something like this
    VB Code:
    1. If Page.IsPostBack Then
    2.    Request.QueryString("id") = ""
    3. End If
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Originally posted by Memnoch1207
    Try something like this
    VB Code:
    1. If Page.IsPostBack Then
    2.    Request.QueryString("id") = ""
    3. End If
    I tried something similar but apparently the QueryString vars are Read-Only

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. If Page.IsPostBack Then
    2. 'Don't read the querystring!
    3. Else
    4. 'whatever else...
    5. End If

    No?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    That works. I forgot IsPostBack would be reset on the link click even when the page is redirecting back to itself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width