7

We are trying to modify the Action attribute of the main ASP.NET form in the master page dynamically.

The page has a radio button, user selects one of the options and clicks on submit, on postback, based on the selection the Form's action attribute should be set and the form should be submitted again automatically.

We were trying to use JavaScript for the same.

document.forms[0].action = "option1.aspx";
document.forms[0].submit();

But this doesn't seem to be working, there is no impact on the action attribute.

if we don't use a master page, this can be achieved easily by using

this.Form.Action = "option1.aspx";
ClientScript.RegisterStartupScript(this.GetType(),"test1",
    "document.form[0].submit();",true);

Sadly, we cant remove the master page .. any pointers on how can this be achieved.. ?

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
Danish Khan
  • 1,893
  • 5
  • 22
  • 35

2 Answers2

4

This is something which I have read they wish they had not done. The Form tag gets its action attribute hardcoded. You have to use a Control Adapter in order to control its construction at runtime. I use it especially for URL Rewriting, when I require the postback URL to be the rewritten one I have created. Scott Gu made the code for it and you can find it here:

http://www.scottgu.com/blogposts/urlrewrite/UrlRewrite_HttpModule1.zip

And the address for the article:

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

REA_ANDREW
  • 10,666
  • 8
  • 48
  • 71
0

Does the form need to pass the values to the resulting page? If not, why don't you just use Response.Redirect to the correct page? For example, presuming you are using a RadioButtonList called lstOptions:

protected void btnSubmit_Click(object sender, EventArgs ags) {
        switch (lstOptions.SelectedValue) {
            case "option1":
                Response.Redirect("~/option1.apsx");
                break;
            //etc
        }
}

If you must pass the values why are you even triggering a post back at all? It sounds like you could accomplish what you want by just using javascript. For example presuming your form is named form1 and your radio buttons have a name of options:

<input type="submit" value="Submit" onclick="javascript:submitForm()" />

<script type="text/javascript">
   function submitForm() {
      for (var i=0; i < document.form1.options.length; i++) {
         if (document.form1.options[i].checked) {
            document.forms[0].action = "option" + i + ".aspx";
            document.forms[0].submit();
         }
      }
   }
</script>
codemonkeh
  • 2,054
  • 1
  • 20
  • 36
  • yes I need to get values from this page.. hence i cant just redirect.. i tried using the option 2 but its not working that way too.. plus the name of the page for the action event is actually generated at run time only.. so its not that straight.. thanks for the help though.. :) – Danish Khan Mar 12 '09 at 15:21