最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the ASP.NET form name - Stack Overflow

programmeradmin1浏览0评论

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();
发布评论

评论列表(0)

  1. 暂无评论