How to overwrite the FORM ACTION attribute

W

wwwmike

How can I overwrite the <FORM action=> attribute?

I can add new attributes with

Dim xx As System.Web.UI.HtmlControls.HtmlForm
xx = Page.FindControl("form1")
xx.Attributes.Add("something", "POST")

....but I can not overwrite the action attribute like (Page.Load event)

xx.Attributes.Add("action", "http://www.somewhere")

this also does not work

xx.Attributes("action") = "http://www.somewhere"

Thanks for helping
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
How can I overwrite the <FORM action=> attribute?

I can add new attributes with

Dim xx As System.Web.UI.HtmlControls.HtmlForm
xx = Page.FindControl("form1")
xx.Attributes.Add("something", "POST")
...but I can not overwrite the action attribute like (Page.Load
event)

xx.Attributes.Add("action", "http://www.somewhere")

this also does not work

xx.Attributes("action") = "http://www.somewhere"

Each ASP.NET Page either posts back to itself or to the URL specified by
the PostbackUrl property of an IButtonControl. You cannot set the action
attribute manually in server-side code.

Cheers,
 
J

Joe (MCAD)

Joerg said:
Thus wrote (e-mail address removed),


Each ASP.NET Page either posts back to itself or to the URL specified by
the PostbackUrl property of an IButtonControl. You cannot set the action
attribute manually in server-side code.

Cheers,

Using trusty reflector, here is the code from the FORM class...

protected override void RenderAttributes(HtmlTextWriter writer)
{
....
writer.WriteAttribute("method", this.Method);
base.Attributes.Remove("method");
writer.WriteAttribute("action", this.GetActionAttribute(), true);
base.Attributes.Remove("action");
....
}
base.EnsureID();
base.RenderAttributes(writer);
}

What we see is that this class is not marked as sealed (YEA!) and that
the action comes from the GetActionAttribute which isnt marked as
virtual (BOOO!) and private (BOOO!). 2 BOOO's outweigh 1 YEA, i.e. MS in
their infinite wisdom decided that you really don't want to be able to
do this. This would be a very good argument on why the web controls
should be open source.

As stated above, the postbackURL is a way to bypass this mechanism. Here
is the code in the framework to do this in the button control...
protected virtual PostBackOptions GetPostBackOptions()
{
PostBackOptions options1 = new PostBackOptions(this, string.Empty);
options1.ClientSubmit = false;
if (this.Page != null)
{
if (this.CausesValidation &&
(this.Page.GetValidators(this.ValidationGroup).Count > 0))
{
options1.PerformValidation = true;
options1.ValidationGroup = this.ValidationGroup;
}
if (!string.IsNullOrEmpty(this.PostBackUrl))
{
options1.ActionUrl =
HttpUtility.UrlPathEncode(base.ResolveClientUrl(this.PostBackUrl));
}
options1.ClientSubmit = !this.UseSubmitBehavior;
}
return options1;
}

The output from this class is then registered with
public string
Page.ClientScript.GetPostBackEventReference(PostBackOptions options,
bool registerForEventValidation)
which will behind the scene change the action attribute for you. I
believe this is the way .NET wants to enforce this kind of functionality
is because you can have multiple forms and such on a page AND within
anyone of those forms you can postback to multiple pages. Here is a blog
entry on how to get this to work...
http://fredrik.nsquared2.com/viewpost.aspx?PostID=222


But then again, this isnt exactly what you were looking for. If you must
change the form action attribute there are two ways.
1. easy way. use javascript on the body's onLoad event and change it there.
2. hard way. capture the output of the rendering before being sent
across and parse it out.

Joe (MCAD)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top