ASP.Net More than 1 form?

2020-04-08 09:13发布

问题:

On every page (sitting in the master) I have the login fields.

In the other pages I have different controls, e.g. a contact formular.

How can I set more than one <form>-tag for this?

回答1:

There is a way to switch forms by using a custom HtmlForm class that can have the form tag rendering toggled on and off. I put out a blog post on how to use this for PayPal and get around the ASP.NET single form tag problem:

http://www.codersbarn.com/post/2008/03/08/Solution-to-ASPNET-Form-PayPal-Problem.aspx

Hope this helps :-)



回答2:

ASP.NET Web Forms are built around the concept of 1-form only. To get around this you'd need to either use a different flavor (a la MVC) or take a look at this article for how to get around it in Web Forms:

http://www.codeproject.com/KB/aspnet/CHtmlForm.aspx



回答3:

Unfortunately with ASPNET Forms you can only have one FORM tag with a runat="server" attribute.

You can, however, have as many FORM tags as you want if you exclude the runat="server" attribute (and as long as they aren't nested within one another).

On your masterpage, for example, you could have a login form at the top of the page and then included the form with runat="server" somewhat lower:

<form action="login" method="post">
  <input type="text" name="username" />
  <input type="password" name="password" />
  <input type="submit" value="Login" />
</form>

<form runat="server">
  ... place server controls here ...
</form>


回答4:

it can't be done with Asp.net web forms(you can have many forms but only one should have a runat="server" which isn't paractical) but you can get it done easely with Asp.net MVC



回答5:

You can use validation groups to split a form into multiple logical forms, so that you can call validation in the Click event handlers for the buttons. This is really all you would need multiple forms for in ASP.Net, unless you were trying to avoid posting the data from all form fields back on postback.

Try the following page sample:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <script runat="server">
        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            Page.Validate("contact");

            if (!Page.IsValid)
                return;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Page.Validate("login");

            if (!Page.IsValid)
                return;
        }
    </script>
    <table>
        <tr>
            <td>
                <asp:Label ID="lblUsername" AssociatedControlID="txtName" runat="server">Username:</asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtName" ValidationGroup="login" runat="server" />
                <asp:RequiredFieldValidator ID="reqName" ControlToValidate="txtName" Display="Dynamic" ErrorMessage="You must supply a username" 
                    ValidationGroup="login" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblPassword" AssociatedControlID="txtPassword" runat="server">Password:</asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtPassword" ValidationGroup="login" runat="server" />
                <asp:RequiredFieldValidator ID="reqPassword" ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="You must supply a password" 
                    ValidationGroup="login" runat="server" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnLogin" Text="Login" ValidationGroup="login" OnClick="btnLogin_Click" runat="server" />
            </td>
        </tr>
    </table>
    <hr />

    <h1>Contact</h1>

    <table>
        <tr>
            <td>
                <asp:Label ID="lblYourName" AssociatedControlID="txtYourName" runat="server">Your name:</asp:Label>
            </td>
           <td>
                <asp:TextBox ID="txtYourName" runat="server" ValidationGroup="contact" />
                <asp:RequiredFieldValidator ID="reqYourName" ControlToValidate="txtYourName" Display="Dynamic" ErrorMessage="Tell us your name!" 
                    ValidationGroup="contact" runat="server" />
           </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblYourEmail" AssociatedControlID="txtYourEmail" runat="server">Your email:</asp:Label>
            </td>
           <td>
                <asp:TextBox ID="txtYourEmail" runat="server" ValidationGroup="contact" />
                <asp:RequiredFieldValidator ID="reqEmail" ControlToValidate="txtYourEmail" Display="Dynamic" ErrorMessage="Tell us your email!" 
                    ValidationGroup="contact" runat="server" />
           </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblMessage" AssociatedControlID="txtMessage" runat="server">Your message:</asp:Label>
            </td>
           <td>
                <asp:TextBox ID="txtMessage" TextMode="MultiLine" runat="server" ValidationGroup="contact" />
                <asp:RequiredFieldValidator ID="reqMessage" ControlToValidate="txtMessage" Display="Dynamic" ErrorMessage="We need you to send us a message!" 
                    ValidationGroup="contact" runat="server" />
           </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnSendMessage" ValidationGroup="contact" Text="Contact" OnClick="btnSendMessage_Click" runat="server" />
            </td>
        </tr>        
    </table>
    </form>
</body>
</html>

Here you get validation, and it behaves as two seperate forms, although the data from both "forms" will be posted back.

This is the standard paradigm for having multiple actions on a page, and has been around since ASP.Net 2 :).

Please forgive the use of inline code, I thought it would make the example easier to follow.



标签: asp.net