Microsoft KB Archive/810218

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

Article ID: 810218

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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.



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.



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 &quot;More Information&quot; section of this article.



STATUS
This behavior is by design.



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=&quot;WebQueryForm&quot; id=&quot;WebQueryForm&quot; method=&quot;post&quot; runat=&quot;server&quot;> //document.all(&quot;WebQueryForm&quot;).action = &quot;Webform1.aspx&quot;; <asp:TextBox id=&quot;TextBox1&quot; style=&quot;Z-INDEX: 101; LEFT: 58px; POSITION: absolute; TOP: 36px&quot; runat=&quot;server&quot; Width=&quot;202px&quot; Height=&quot;28px&quot;></asp:TextBox> <P><asp:Button ID=&quot;Button1&quot; Runat=&quot;server&quot; Text=&quot;Submit&quot; Width=&quot;93px&quot; style=&quot;Z-INDEX: 102; LEFT: 55px; POSITION: absolute; TOP: 84px&quot;></asp:Button></P> <asp:HyperLink ID=&quot;Hyperlink1&quot; Runat=&quot;server&quot; NavigateUrl=&quot;webform1.aspx?Name=Joe&quot; style=&quot;Z-INDEX: 103; LEFT: 58px; POSITION: absolute; TOP: 125px&quot;> Get URL for this View </asp:HyperLink> </li> In the editor, right-click the Webform1.aspx page, and then click View Code. The code-behind page appears in the editor.</li> If QueryString exists, turn off SmartNavigation to permit the postback to change the URL.</li>  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 = &quot;http://localhost/WebQueryString/webform1.aspx?Name=&quot; & Me.TextBox1.Text Else Me.Hyperlink1.NavigateUrl = &quot;http://localhost/WebQueryString/webform1.aspx&quot; End If       If (Request.QueryString(&quot;&quot;) <> &quot;&quot;) Then Me.SmartNavigation = False End If

If Request.QueryString(&quot;Name&quot;) <> Nothing Then Me.TextBox1.Text = Request.QueryString(&quot;Name&quot;) End If

Visual C# .NET Sample
if(this.TextBox1.Text.Length > 0) {       this.Hyperlink1.NavigateUrl = &quot;http://localhost/WebQueryString/WebForm1.aspx?Name=&quot; + this.TextBox1.Text; }   else {       this.Hyperlink1.NavigateUrl = &quot;http://localhost/WebQueryString/WebForm1.aspx&quot;; }

if(Request.QueryString.ToString != &quot;&quot;) {       this.SmartNavigation = false; }

if(Request.QueryString[&quot;Name&quot;] != null) {       this.TextBox1.Text = Request.QueryString[&quot;Name&quot;]; }

Note If SmartNavigation is turned on while QueryString exists, SmartNavigation does not submit the form and you cannot modify the URL. </li> On the File menu, click Save All to save the Web Form and other associated project files.</li> On the Build menu, click Build to build the project.</li> In Solution Explorer, right-click the Webform1.aspx page, and then click View in Browser to open the page in the Web browser.</li> 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 </li> 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.</li> 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.</li></ol>

To work around this behavior, follow these steps: <ol>  In the sample code that you pasted in step 7 of the &quot;Steps to Reproduce the Behavior&quot; section, uncomment the following JScript .NET line: document.all(&quot;WebQueryForm&quot;).action = &quot;Webform1.aspx&quot;; </li> On the File menu, click Save All to save the Web Form and other associated project files.</li> On the Build menu, click Build to build the project.</li> In Solution Explorer, right-click the Webform1.aspx page, and then click View in Browser to open the page in the Web browser.</li> 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 </li> 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.</li> <li>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.</li></ol>

<div class="references_section">