in

ASP.NET Weblogs

uber1024's WebLog

It's not hot wings and beer, but it's still okay

How to render form tag with action=""

I've seen a couple of posts to ASP.NET forums to the effect of “URLRewriting is dumb because the action attribute of the form tag will point to the wrong place.”  What you need to do is to create your own version of the HtmlTextWriter class that overrides the default behavior.  Here's what I came up with:

public class MyWriter: HtmlTextWriter
{
  bool blnIsForm = false;
  private TextWriter writer;
  public MyWriter(TextWriter writer): base(writer)
  {
    this.writer = writer;
  }
  public MyWriter(TextWriter writer, string tabString): base(writer, tabString)
  {
   this.writer = writer;
  }
  public override void RenderBeginTag(string tagName)
  {
    blnIsForm = String.Compare(tagName,"form") == 0;
    base.RenderBeginTag (tagName);
  }
  public override void WriteAttribute(string name, string value, bool fEncode)
  {
    if(String.Compare(name, "action", true) == 0)
    {
      value = "";
    }
    base.WriteAttribute (name, value, fEncode);
  }
}

So there you have it Jimmy-Jimmy.  The important parts of this are the RenderBeginTag checks to see if it is currently rendering a form tag and the WriteAttribute checks to see if it is rendering the action attribute and, if so, it just changes the actual value with an empty string.  In a later post, I will show how to call this new writer from the Page object.

By the way ... does anyone have a better way to insert code into a post than pasting it in and then modifying the HTML?

Comments

 

Daniel Jin said:

derive from the HtmlForm control, and override the RenderAttributes method. a lot more efficient than your method. here's the article about it http://msdn.microsoft.com/asp.net/using/building/web/default.aspx?pull=/library/en-us/dnaspp/html/URLRewriting.asp
April 8, 2004 8:53 AM
 

uber said:

I thought of that, but you lose the elegance of having just <form runat=server> in your code. My designers will see the form and be able to understand the implications on their design, whereas when they see Register directives and user controls, their eyes start to glaze over.

I wouldn't go so far as to say that deriving from HtmlForm is A LOT more efficient. Maybe I'll try them both and test the performance of each method and post the results.
April 8, 2004 9:54 AM
 

Daniel Jin said:

> I wouldn't go so far as to say that deriving from HtmlForm is A LOT more efficient.

well, I don't know how you are calling this writer from the page since you didn't show the code. but I assumed that this writer is used for every tag generated on the page, that's why you are comparing if the tag currently writing is a form tag or not. if this assumption is true, the of course deriving from HtmlForm is a lot more efficient. If the assumption is false, then I may be wrong.
April 8, 2004 11:03 AM
 

uber said:

> if this assumption is true, the of course deriving from HtmlForm is a lot more efficient.

The assumption is true, and I'm just saying that I'd probably test the assertion before saying something is a LOT more efficient. I won't argue that it's more efficient, but with everything else that's happening in the rendering of the page (database access across a network, databinding, checking to see if the user is banned, looking up the forum name, etc), I don't think that a few thousand comparisons are going to make all that much of a difference.

I definitely want the application to perform, but there are probably a hundred other places where I could make improvements before I get to this one. But, hey, we're developers and this is what we spend our time thinking about, so the discussion is definitely a good one! Thanks for starting it, Daniel!

And I was going to show the code in my next ASP.NET post, probably later today.
April 8, 2004 11:11 AM
 

Jerry Pisk said:

I also hope that you know that <form> tag with an action other than http uri is invalid in HTML. And an empty string is not a valid http uri.
April 8, 2004 1:10 PM
 

uber said:

> I also hope that you know that <form> tag with an action other than http uri is invalid in HTML.

I know that, but almost every browser that has a market share of more than about 2% supports this behavior ... so I'm willing to break the rules in order to get things done. If I made a list of things that were important to me, producing strictly valid HTML would rank right below cleaning under my refridgerator and right above scraping other people's gum off the street in front of my apartment.

I'm not saying it's bad to be concerned with that, but invalid HTML that works at least 98% of the time is okay with me.
April 8, 2004 1:17 PM
 

TrackBack said:

April 8, 2004 9:21 PM

Leave a Comment

(required)  
(optional)
(required)  
Add