admin管理员组

文章数量:1314252

I'm looking for a way to get the name of the main HTML form so I can submit it from JavaScript.

The reason I can just set the name of the form is because the JavaScript is on a User Control that could get added to many different sites with different form names.

Thanks.

I'm looking for a way to get the name of the main HTML form so I can submit it from JavaScript.

The reason I can just set the name of the form is because the JavaScript is on a User Control that could get added to many different sites with different form names.

Thanks.

Share Improve this question asked Oct 29, 2008 at 1:09 Ryan SmithRyan Smith 8,33422 gold badges78 silver badges103 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

I'm not totally sure that this will address what you're asking for, so please ment on it:

In your script, when the User Control renders, you could have this placed in there. So long as script doesn't have a "runat" attribute, you should be good.

<script type="text/javascript">

var formname = '<%=this.Page.Form.Name %>';

</script>

I'm not sure , try "YourUserControl.Page.Form"

But why do you need the form name, are you going to have more than one form on your .aspx page , if not, you can get the whole post back JS code for the control that will do the post back (submit) using "Page.ClientScript.GetPostBackEventReference()" and use it to set the "OnClick" attribute of whatever you will use to submit the page.

you can't have more than one form control with runat="server" on an aspx page, so you cn use document.forms[0]

<script type="text/javascript">

var formname = '<%=this.Page.AppRelativeVirtualPath.Replace(this.Page.AppRelativeTemplateSourceDirectory,"")%>';

</script>
    

ASP.NET pages can only have one form, so its safe to just do:

  document.forms[0].submit();

本文标签: javascriptGet the ASPNET form nameStack Overflow