Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: Cannot Change or Remove the QueryString Action for a Web Form on Postback


View products that this article applies to.

Symptoms

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


Cause

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


Workaround

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


Status

This behavior is by design.

↑ Back to the top


More information

Steps to Reproduce the Behavior

  1. Open Microsoft Visual Studio .NET.
  2. On the File menu, point to New and then click Project.
  3. Under Project Type, click Visual Basic Projects or Visual C# Projects.
  4. Under Templates, click ASP.NET Web Application. Name the application WebQueryString.
  5. Webform1.aspx page is displayed by default.
  6. 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>&nbsp;</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>
    
  7. In the editor, right-click the Webform1.aspx page, and then click View Code. The code-behind page appears in the editor.
  8. If QueryString exists, turn off SmartNavigation to permit the postback to change the URL.
  9. 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.
  10. On the File menu, click Save All to save the Web Form and other associated project files.
  11. On the Build menu, click Build to build the project.
  12. In Solution Explorer, right-click the Webform1.aspx page, and then click View in Browser to open the page in the Web browser.
  13. 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
  14. 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.
  15. 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:
  1. 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>
    
  2. On the File menu, click Save All to save the Web Form and other associated project files.
  3. On the Build menu, click Build to build the project.
  4. In Solution Explorer, right-click the Webform1.aspx page, and then click View in Browser to open the page in the Web browser.
  5. 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
  6. 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.
  7. 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


References

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


Article Info
Article ID : 810218
Revision : 8
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 424