Multiple "Submit" Buttons & Form Fields

G

Guest

In ASPX 2.0 with MasterPages and all that, my entire page only has one actual
<FORM>. But there are several different sections of the page that provide
what are functionally separate forms with separate Submit buttons.

In basic HTML, I can have multiple forms on a page and the browser knows
that if any of the fields for Form A have the focus, then pressing ENTER will
send a Click to the submit button for Form A. Same for Form B, etc.

Since there's only one form in ASPX, how do I do this? Do I have to manage
all this with JavaScript and if so, do I have to write it myself or do the
controls have any built-in features for doing this?

Alex
 
B

Brock Allen

The model in ASP.NET is to simply to create different button click handlers
for the various buttons.
 
G

Guest

Yes but this isn't quite what I'm talking about: I'm talking about which of
the buttons effectively receives the client-side click based on which related
text box had the focus at the time that the ENTER key was pressed. In the old
multiple-forms approach in HTML, the browser handled this automatically.

Alex
 
B

Brock Allen

Ah, I see what you're saying. In 2.0 there's the ability to set a default
button, but that's still not what you want. You'd have to see what control
has focus upon submit and pass that info along to the server. You can use
Page.ClientScript.RegisterHiddenField and Page.ClientScript.RegisterOnSubmitStatement
to accomplish this.
 
S

Steven Cheng[MSFT]

Thanks for Brock's input,

Hi Alex,

As for multiple <form> support on ASP.NET page, it is somewhat a limitation
due to the original design of ASP.NET page. ASP.NET page use a single
runat="server" form to centralize all the server controls, this is very
important for the ASP.NET server-side to control the postback events for
the server controls(rely on such single server <form> structure...).
However, if want to want to to add is normal html <form> (not
runat=server), that is still ok. For example:

====================
<body>
<form id="form1" runat="server">
<div>
<select id="lst" runat="server" onchange="handlechange();" >
<option title="item1" value="item1">item1</option>
<option title="item2" value="item2">item2</option>
<option title="item3" value="item3">item3</option>
</select>

<br />
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
<form id="form2" action="http://www.asp.net">
<input name="txtf2" type="text" />
</form>
</body>
</html>
====================

Anyway, only one "runat=server" element is allowed in single ASP.NET page.
Also, normal <form> can not be nested with each other.

In addition, if the reason you use multiple html <form> in single ASP.NET
page is just to post data to different page or address, you can consider
using the ASP.NET 2.0's new "cross page posting" feature.

#Cross-Page Posting in ASP.NET Web Pages
http://msdn2.microsoft.com/en-US/library/ms178139.aspx

#Design Considerations for Cross Page Post Backs in ASP.NET 2.0
http://www.odetocode.com/Articles/421.aspx

For the "default button" behavior, I'm afraid so far ASP.NET page only
support set such default submit button (when press "enter" key) for the
runat="server" form. For other form, you may still use own client-side
script to do this. For example, we can add the onkeydown or onkeypress
client-side script event for the <form> or <input type="text" > field, and
check the key code, it's enter key, call the form element's submit method.
Here are some articles mentioned such script:

http://jennifermadden.com/javascript/stringEnterKeyDetector.html

http://www.cs.tut.fi/~jkorpela/forms/enter.html

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Alex,

Does those suggestion helps a little? Please feel free to post here if
there is anything else we can help.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top