ASP.NET Tutorial/HTML Controls/Form — различия между версиями

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

Версия 15:30, 26 мая 2010

A survey form (C#)

<!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 + "<br />";
        feedbackLabel.Text += "Your email is: " + email.Value +
            "<br />";
        feedbackLabel.Text += "You like to work with:<br />";
        for (int i = 0; i <= serverModel.Items.Count - 1; i++) 
        {
          if (serverModel.Items[i].Selected) 
          {
            feedbackLabel.Text += " - " + serverModel.Items[i].Text +
                "<br />";
          }
        }
        feedbackLabel.Text += "You like .NET: " + likeDotNet.Value;
      }
    </script>
  </head>
  <body>
    <form runat="server">
      <h2>Take the Survey!</h2>
        Name:<br />
        <input type="text" id="name" runat="server" />
        Email:<br />
        <input type="text" id="email" runat="server" />
        Which server technologies do you use?<br />
        <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?<br />
        <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>


form default focus

<%@ 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">
    <div>
    TextBox1:
    <asp:textbox id="TextBox1"
                 runat="server" 
                 AccessKey="1"></asp:textbox>
    <br />
    TextBox2:
    <asp:textbox id="TextBox2"
                 runat="server" 
                 AccessKey="2"></asp:textbox>
    <br /><br />
    <asp:button id="cmdSetFocus1"
                text="Set Focus #1" 
                runat="server" 
                OnClick="cmdSetFocus1_Click">
    </asp:button>&nbsp;
    <asp:button id="cmdSetFocus2" 
                text="Set Focus #2"
                runat="server" 
                OnClick="cmdSetFocus2_Click" >
    </asp:button>&nbsp;
    <asp:Button ID="cmdSubmit" 
                runat="server" 
                Text="Submit" 
                OnClick="cmdSubmit_Click" />
    <hr />
    <asp:label id="Label1"
               runat="Server" 
               EnableViewState="False"></asp:label>
 </div>
</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();
  }
}


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

<%@ 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>
   
    <div id="pageContent">
        <form id="step0" runat="server" visible="true">
            <h1>Welcome</h1>
            <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">
            <h1>Step #1</h1>
            <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">
            <h1>Finalizing</h1>
            <asp:button ID="Button4" runat="server" text="Finish" OnClick="Button4_Click" />
        </form>
    </div>
</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("<h1>Successfully done.</h1>");
  }
}


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

<%@ 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>
    <div id="pageContent">
        <table>
        <tr>
        <td valign="top"> 
        <form id="form1" runat="server">
        <div>
            <h2>Any collection of controls here</h2>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <h3>This represents an ordinary ASP.NET page</h3>
        </div>
        </form>
        
        </td>
        <td valign="top" align="right" > 
            <form method="post" action="search.aspx">
                <h3>Search Box</h3>
                <table>
                <tr >
                    <td>Keyword</td>
                    <td><input type="text" id="Keyword" name="Keyword" /></td>
                </tr>
                <tr>
                    <td colspan="2">
                    <input type="submit" id="StartSearch" value="Search" />
                    </td>
                </tr>
                </table>
            </form>
        </td>
        </tr>
        </table>
    </div>
</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">
    <div id="pageContent">
    <h1>Searching for...  
        <asp:Label ID="KeywordBeingUsed" runat="server" ForeColor="blue"></asp:Label></h1>
    </div>
    </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;
    }
}


Specifying a Default Button

<%@ 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">
    <div>
    <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" />
    <hr />
    <asp:Label
        id="lblResult"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Submitting Form Data

<%@ 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">
    <div>
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        OnClick="btnSubmit_Click"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblTime"
        Runat="server" />
    </div>
    </form>
</body>
</html>


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

<%@ 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: <b>" & User.Text & _
         "</b><br>" & "Password: <b>" & Pass.Text & "</b>"
   end sub
</script> 
<html><body>
   <form runat="server">
      <table>
         <tr>
            <td><b>Login: </b></td>
            <td><ASP:TextBox id="User" runat="server"/></td>
         </tr>
         <tr>
            <td><b>Password: </b></td>
            <td><ASP:TextBox id="Pass" TextMode="Password" 
                   runat="server"/></td>
         </tr>
         <tr>
            <td></td>
            <td><ASP:Button Text="Submit" runat="server" 
                   OnClick="Submit" /></td>
         </tr>
      </table>
      
      <ASP:Label id="Label1" runat="server"/>
   </form>
</body></html>


Using validation controls to fill a form.

<%@ 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>
    <div id="pageContent">
        <form id="form1" runat="server">
          <table>
              <tr>
                  <td>Name</td><td>*</td>
                <td><asp:textbox runat="server" id="fname" />
                    <asp:RequiredFieldValidator runat="server" id="fnameValidator" 
                        ControlToValidate="fname" 
                      Text="!!!"
                        ErrorMessage="Name is mandatory" /></td></tr>
              <tr>
                  <td>Last Name</td><td>*</td>
                <td><asp:textbox runat="server" id="lname" />
                    <asp:RequiredFieldValidator runat="server" id="lnameValidator" 
                        ControlToValidate="lname" 
                      Text="!!!"
                        ErrorMessage="Last name is mandatory" /></td></tr>
              <tr>
                  <td>Age</td><td></td>
                <td><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." /></td></tr>
              <tr>
                  <td>Hire Date</td><td></td>
                <td><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." /></td></tr>
              <tr>
                  <td>Membership Level</td><td></td>
                <td><asp:textbox runat="server" id="membership" />
                    <asp:CustomValidator runat="server" id="membershipValidator" 
                         ControlToValidate="membership" 
                       ClientValidationFunction="CheckMembership"
                         ErrorMessage="Must be Gold or Platinum." /></td></tr>
          </table>
          
          <asp:linkbutton ID="Linkbutton1" runat="server" Text="Add..." />
            <hr>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server"  
            ShowMessageBox="true" 
            ShowSummary="true" 
            HeaderText="The following errors occurred:"
            DisplayMode="BulletList" />
        </form>
    </div>
</body>
</html>