views:

1160

answers:

4

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.

+2  A: 

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.

Mohamed Faramawi
+2  A: 

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

  document.forms[0].submit();
FlySwat
I believe you can have more than one form on the page.. although you can only have one form with a runat=server tag
Paul Rowland
Well, yeah, if you want to split hairs. You can't nest them though.
FlySwat
but that would mean document.forms[0].submit(); isnt correct
Paul Rowland
I would wager it means that the original design philosophy is incorrect :)
FlySwat
A: 

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

+2  A: 

I'm not totally sure that this will address what you're asking for, so please comment 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>
Andrew Theken
Thanks, That's exactly what I was looking for.
Ryan Smith