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.
↑ Back to the top
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.
↑ Back to the top
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.
↑ Back to the top
Steps to Reproduce the Behavior
- Open Microsoft Visual Studio .NET.
- On the File menu, point to
New and then click Project.
- Under Project Type, click Visual
Basic Projects or Visual C# Projects.
- Under Templates, click ASP.NET Web
Application. Name the application WebQueryString.
- Webform1.aspx page is displayed by
default.
- In the HTML view of
Webform1.aspx, Paste the following sample code to replace the
existing HTMLForm tag between the HTMLBody tags.
<form name="WebQueryForm" id="WebQueryForm" method="post" runat="server">
<script>
//document.all("WebQueryForm").action = "Webform1.aspx";
</script>
<p><asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 58px; POSITION: absolute; TOP: 36px" runat="server" Width="202px" Height="28px"></asp:TextBox></p>
<P><asp:Button ID="Button1" Runat="server" Text="Submit" Width="93px" style="Z-INDEX: 102; LEFT: 55px; POSITION: absolute; TOP: 84px"></asp:Button></P>
<p> </p>
<p><asp:HyperLink ID="Hyperlink1" Runat="server" NavigateUrl="webform1.aspx?Name=Joe" style="Z-INDEX: 103; LEFT: 58px; POSITION: absolute; TOP: 125px"> Get URL for this View </asp:HyperLink></p>
</form>
- 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
Visual C# .NET Sample
if(this.TextBox1.Text.Length > 0)
{
this.Hyperlink1.NavigateUrl = "http://localhost/WebQueryString/WebForm1.aspx?Name=" + this.TextBox1.Text;
}
else
{
this.Hyperlink1.NavigateUrl = "http://localhost/WebQueryString/WebForm1.aspx";
}
if(Request.QueryString.ToString() != "")
{
this.SmartNavigation = false;
}
if(Request.QueryString["Name"] != null)
{
this.TextBox1.Text = Request.QueryString["Name"];
}
Note If SmartNavigation is turned on while QueryString exists, SmartNavigation does not submit the form and you cannot
modify the URL. - On the File menu, click Save
All to save the Web Form and other associated project files.
- On the Build menu, click
Build to build the project.
- In Solution Explorer, right-click the
Webform1.aspx page, and then click View in
Browser to open the page in the Web browser.
- After you view the page, type a QueryString value at the end of the URL as shown in the following example,
and then press
ENTER:
http://localhost/WebQueryString/Webform1.aspx?Name=Jeo - 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:
<script>
document.all("WebQueryForm").action = "Webform1.aspx";
</script>
- On the File menu, click Save
All to save the Web Form and other associated project files.
- On the Build menu, click
Build to build the project.
- In Solution Explorer, right-click the
Webform1.aspx page, and then click View in
Browser to open the page in the Web browser.
- Type a QueryString value at the end of the URL as shown in the following example,
and then press
ENTER:
http://localhost/WebQueryString/Webform1.aspx?Name=Jeo - 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.
↑ Back to the top
For more information about ASP.NET, visit the following
MIcrosoft Web site:
For tutorials about the Microsoft .NET Framework and Visual
Studio .NET, visit the following Microsoft Web site:
↑ Back to the top