ASP.NET Tutorial/HTML Controls/Form

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A survey form (C#)

   <source lang="csharp">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

 <head>
   <title>Using ASP.NET HTML Server Controls</title>
   <script runat="server" language="C#">
     void Click(Object s, EventArgs e)
     {
       feedbackLabel.Text = "Your name is: " + name.Value + "
"; feedbackLabel.Text += "Your email is: " + email.Value + "
"; feedbackLabel.Text += "You like to work with:
"; for (int i = 0; i <= serverModel.Items.Count - 1; i++) { if (serverModel.Items[i].Selected) { feedbackLabel.Text += " - " + serverModel.Items[i].Text + "
"; } } feedbackLabel.Text += "You like .NET: " + likeDotNet.Value; } </script> </head> <body> <form runat="server">

Take the Survey!

       Name:
<input type="text" id="name" runat="server" /> Email:
<input type="text" id="email" runat="server" /> Which server technologies do you use?
<select id="serverModel" runat="server" multiple="true"> <option>ASP.NET</option> <option>PHP</option> <option>JSP</option> <option>CGI</option> <option>ColdFusion</option> </select> Do you like .NET so far?
<select id="likeDotNet" runat="server"> <option>Yes</option> <option>No</option> </select> <button id="confirmButton" OnServerClick="Click" runat="server">Confirm</button> <asp:Label id="feedbackLabel" runat="server" /> </form> </body></source>

form default focus

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FocusAndDefault" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Focus and Default Button</title>

</head> <body>

   <form id="Form1"
         defaultbutton="cmdSubmit" 
         defaultfocus="TextBox1"
         runat="server">
   TextBox1:
   <asp:textbox id="TextBox1"
                runat="server" 
                AccessKey="1"></asp:textbox>
   
TextBox2: <asp:textbox id="TextBox2" runat="server" AccessKey="2"></asp:textbox>

<asp:button id="cmdSetFocus1" text="Set Focus #1" runat="server" OnClick="cmdSetFocus1_Click"> </asp:button>  <asp:button id="cmdSetFocus2" text="Set Focus #2" runat="server" OnClick="cmdSetFocus2_Click" > </asp:button>  <asp:Button ID="cmdSubmit" runat="server" Text="Submit" OnClick="cmdSubmit_Click" />

   <asp:label id="Label1"
              runat="Server" 
              EnableViewState="False"></asp:label>

</form>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class FocusAndDefault : System.Web.UI.Page {

 protected void cmdSubmit_Click(object sender, EventArgs e)
 {
   Label1.Text = "Clicked Submit.";
 }
 protected void cmdSetFocus1_Click(object sender, EventArgs e)
 {
   TextBox1.Focus();
 }
 protected void cmdSetFocus2_Click(object sender, EventArgs e)
 {
   TextBox2.Focus();
 }

}</source>


Multiple server forms can be employed as long as only one is rendered at a time

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

   Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Multiple Server Forms--Kind of Wizard</title>

</head> <body>

       <form id="step0" runat="server" visible="true">

Welcome

           <asp:textbox runat="server" id="Textbox1" />
           <asp:button ID="Button1" runat="server" text="Step #1" OnClick="Button1_Click" />
       </form>
       <form id="step1" runat="server" visible="false">

Step #1

           <asp:textbox runat="server" id="Textbox2" />
           <asp:button ID="Button2" runat="server" text="Previous step" OnClick="Button2_Click" />
           <asp:button ID="Button3" runat="server" text="Step #2" OnClick="Button3_Click" />
       </form>
       <form id="step2" runat="server" visible="false">

Finalizing

           <asp:button ID="Button4" runat="server" text="Finish" OnClick="Button4_Click" />
       </form>

</body> </html>

File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;

public partial class Default : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
   Title = "Welcome";
   }
 protected void Button1_Click(object sender, EventArgs e)
 {
   Title = "Step 1";
   step0.Visible = false;
   step1.Visible = true;
 }
 protected void Button2_Click(object sender, EventArgs e)
 {
   step0.Visible = true;
   step1.Visible = false;
 }
 protected void Button3_Click(object sender, EventArgs e)
 {
   Title = "Finalizing";
   step1.Visible = false;
   step2.Visible = true;
 }
 protected void Button4_Click(object sender, EventArgs e)
 {
   Title = "Done";
   step2.Visible = false;
Response.Write("

Successfully done.

");
 }

}</source>


one server-side form tag and multiple client HTML form elements

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Multiple HTML Forms</title>

</head> <body>

       <form id="form1" runat="server">

Any collection of controls here

           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
           <asp:Button ID="Button1" runat="server" Text="Button" />

This represents an ordinary ASP.NET page

       </form>
       
           <form method="post" action="search.aspx">

Search Box

Keyword <input type="text" id="Keyword" name="Keyword" />
                   <input type="submit" id="StartSearch" value="Search" />
           </form>

</body> </html> File: NextPage.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NextPage.aspx.cs"

   Inherits="NextPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Search Page</title>

</head> <body>

   <form id="form1" runat="server">

Searching for... <asp:Label ID="KeywordBeingUsed" runat="server" ForeColor="blue"></asp:Label>

   </form>

</body> </html> File: NextPage.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

public partial class NextPage : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
   string textToSearch = Request.Form["Keyword"].ToString();
   KeywordBeingUsed.Text = textToSearch;
   }

}</source>


Specifying a Default Button

   <source lang="csharp">

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   protected void btnSearch_Click(object sender, EventArgs e)
   {
       lblResult.Text = "Search for: " + txtSearch.Text;
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Button Default Button</title>

</head> <body>

   <form id="form1" defaultbutton="btnSearch" runat="server">
   <asp:Label
       id="lblSearch"
       Text="Search:"
       AssociatedControlID="txtSearch"
       Runat="server" />
       
   <asp:TextBox
       id="txtSearch"
       Runat="server" />
       
   <asp:Button
       id="btnSearch"
       Text="Search"
       OnClick="btnSearch_Click"
       Runat="server" />
       
   <asp:Button
       id="btnCancel"
       Text="Cancel"
       Runat="server" />

   <asp:Label
       id="lblResult"
       Runat="server" />
   </form>

</body> </html></source>


Submitting Form Data

   <source lang="csharp">

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   protected void btnSubmit_Click(object sender, EventArgs e)
   {
       lblTime.Text = DateTime.Now.ToString("T");
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show Button</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Button
       id="btnSubmit"
       Text="Submit"
       OnClick="btnSubmit_Click"
       Runat="server" />
   

<asp:Label id="lblTime" Runat="server" />
   </form>

</body> </html></source>


Use HTML form to layout asp.net controls (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

  dim Username as string
  dim Password as string
  dim BackColor as string
  sub Submit(Sender as Object, e as EventArgs)
     Label1.Text = "Username: " & User.Text & _
        "
" & "Password: " & Pass.Text & "" end sub

</script> <html><body>

  <form runat="server">
Login: <ASP:TextBox id="User" runat="server"/>
Password: <ASP:TextBox id="Pass" TextMode="Password" runat="server"/>
<ASP:Button Text="Submit" runat="server" OnClick="Submit" />
     <ASP:Label id="Label1" runat="server"/>
  </form>

</body></html></source>


Using validation controls to fill a form.

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Form filling (ASP.NET)</title>

</head> <script type="text/javascript"> function CheckMembership(source, arguments) {

 arguments.IsValid = false;
 var buf = arguments.Value;
 if (buf == "Normal" || buf== "Silver" ||
           buf == "Gold" || buf == "Platinum")
   arguments.IsValid = true;

} </script>

<body>

       <form id="form1" runat="server">
Name* <asp:textbox runat="server" id="fname" />
                   <asp:RequiredFieldValidator runat="server" id="fnameValidator" 
                       ControlToValidate="fname" 
                     Text="!!!"
ErrorMessage="Name is mandatory" />
Last Name* <asp:textbox runat="server" id="lname" />
                   <asp:RequiredFieldValidator runat="server" id="lnameValidator" 
                       ControlToValidate="lname" 
                     Text="!!!"
ErrorMessage="Last name is mandatory" />
Age <asp:textbox runat="server" id="age" />
                   <asp:CompareValidator runat="server" id="ageValidator" 
                       ControlToValidate="age" 
                     Operator="GreaterThanEqual" 
                     ValueToCompare="18"
                       Type="integer"
ErrorMessage="Age must be at least 18." />
Hire Date <asp:textbox runat="server" id="hired" />
                   <asp:CompareValidator runat="server" id="hiredValidator" 
                       ControlToValidate="hired" 
                     Display="Static"  
                     Operator="DataTypeCheck" 
                       Type="date"
                       ErrorMessage="Must enter a date." />
                   <asp:RangeValidator runat="server" id="hiredDateValidator" 
                       ControlToValidate="hired" 
                     Display="Dynamic"
                       MinimumValue="1999-1-1"
                     MaximumValue="9999-12-31"
                       Type="Date"
ErrorMessage="Date after 1-1-99." />
Membership Level <asp:textbox runat="server" id="membership" />
                   <asp:CustomValidator runat="server" id="membershipValidator" 
                        ControlToValidate="membership" 
                      ClientValidationFunction="CheckMembership"
ErrorMessage="Must be Gold or Platinum." />
         <asp:linkbutton ID="Linkbutton1" runat="server" Text="Add..." />

           <asp:ValidationSummary ID="ValidationSummary1" runat="server"  
           ShowMessageBox="true" 
           ShowSummary="true" 
           HeaderText="The following errors occurred:"
           DisplayMode="BulletList" />
       </form>

</body> </html></source>