Vikasumit

How To: Change Form Action in Asp.Net 1.1
Author : Sumit Gupta

As I start my First application in Asp.Net, I have to integrate the SEO friendly URLs for it. I did that successfully. But now I start getting problem with it as the POST back in Asp.Net goes to the same page only, and on few occassion it doesn't give me full command over the page output and hence I come with solution that change the Default form action.

I search for this, with hope that it is possible in ASP.Net. I found few articles suggesting that it can be changed using Javascript, but what if Javascript is not enable on user machine. So, I keep searching and find few article on this, they are quite good and they help me in creating my own class, which I am going to share with you in this article.

Following is the Code I have used

Code is very simple in itself. Let me Quickly explain the code what we achieve with this.
I have simply make a new class that is child class of HtmlTextWriter, this class is used by Asp.Net to generate the final output before sending that to the browser. By driving this class, I am going to intercept the Call where ASP.Net is about to put his own default action attribute for form tag.

Sub RenderBeginTag is called before every Tag render begin so we just intercept it for "Form" tag call. Function/sub that is of our concern is WriteAttribute which is responsible for writing all the Tags attribute. So, I check the tag name if it is "action" simply set it to my default action which come in MyAction property. Now I just need to put below code in my Code behind of page for which I need to change the Form Action

Now, now what ... its finish you have change the form action simple. Just use the Above class and include a VB class module and write this simple 4 line code and change the form action.

Article Correction (17th Feb 2006)
I Forget to mention that in Sub Render function of the Page you need to give your "Page URL" you want your Form will be submitted. You need to change "HttpContext.Current.Items("VirtualPath")" with your Page URL. This Correction comes as a result of Mr. Veeru who mailed me about his problem that he is not able to submit the form using above class.

Also, please do not use this Class to submit to another ASP.Net page as otherwise it will result in ViewState Error. I need to find the patch for this, but as of now I don't know that myself. If you know the solution for that please do mention me.