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)