When you try to change the target of the postback by
including an action attribute in the HtmlForm tag, ASP.NET overrides the action
attribute and forces the form to post back to itself. This behavior forces all
the postbacks to hold the same QueryString.
The action property of the HtmlForm tag is always the
current page. Although this change was intended to prevent cross-page changes
to the URL in a Web Form, this also prevents the change in the QueryString of a Web Form. You cannot change or remove the QueryString of a Web Form.
To work around this behavior, use client-side scripting to
handle the body.onload event, and then set the Action attribute of HtmlForm to strip the QueryString. For more information about how to do this, see the "More
Information" section of this article.
In the editor, right-click the
Webform1.aspx page, and then click View Code.
The code-behind page appears in the editor.
If QueryString exists, turn off
SmartNavigation to permit the postback to change the URL.
Paste the appropriate code sample for your programming
language in the Page_Load event:
Visual Basic .NET Sample
If (Me.TextBox1.Text.Length > 0) Then
Me.Hyperlink1.NavigateUrl = "http://localhost/WebQueryString/webform1.aspx?Name=" & Me.TextBox1.Text
Else
Me.Hyperlink1.NavigateUrl = "http://localhost/WebQueryString/webform1.aspx"
End If
If (Request.QueryString("") <> "") Then
Me.SmartNavigation = False
End If
If Request.QueryString("Name") <> Nothing Then
Me.TextBox1.Text = Request.QueryString("Name")
End If
Reload the page in the browser. The value for the QueryString variable appears in the Textbox box. This is the
value that you can use to return to a previous state for a page.
Change the value in the Textbox box, and
then click Submit. The QueryString value overwrites the value in the Textbox box.
The QueryString value is not stripped out.
To work around this behavior, follow these steps:
In the sample code that you pasted in step 7 of the "Steps
to Reproduce the Behavior" section, uncomment the following JScript .NET line:
Reload the page in the browser. The value for the QueryString variable appears in the Textbox box. This is the
value that you can use to return to a previous state for a page.
Change the value in the Textbox box to a
different string, and then click Submit. The QueryString value does not overwrite the value in the
Textbox box, and the QueryString value is stripped out.