1.  服务器控件

ASP.NET控件分为服务器控件和HTML标记。

服务器控件是在服务器端运行的执行程序逻辑的组件,服务器端的程序可以访问这类控件;而HTML标记是在客户端运行的,服务器端程序不能访问这类控件。

服务器控件编程的关键是runat属性,如果一个控件使用了runat="server"属性进行声明,则该控件被认为是服务器控件。

工具箱的“HTML”选项卡中的HTML控件都是HTML标记,可以通过加上runat="server"属性将它们改为服务器控件。
ASP.NET服务器控件又分为两大类:Web服务器控件和HTML服务器控件。

1..1  Web服务器控件的基本属性

Web服务器控件位于System.Web.UI.WebControl命名空间中,是从WebControl基类直接或间接派生的。

Web服务器控件的属性可以通过“属性”窗口来设置,也可以通过HTML代码实现。Web服务器控件以“asp:”为前缀,ID属性指定其ID值,作为控件的唯一标识。基本属性可为布局、行为、可访问性、外观等几类。

2 基本的Web服务器控件

2.1 Label控件又称为标签控件,用于显示静态文本。其主要的属性是Text,用于设置或获取该控件的显示文本。
仅当需要在服务器代码中更改文本内容或其他特性时,才使用Label控件

2.2 TextBox控件

TextBox控件是用于向Web页面输入信息的最常用的控件。默认为单行文本框,可通过TextMode属性来改变它的文本显示模式,该属性是TextBoxMode枚举类型的属性值,具有如下三种可选值。
①SingleLine:表示单行输入模式。
②MultiLine:表示多行输入模式。
③PassWord:表示密码输入模式。

例:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="W5_3_2_1.aspx.cs" Inherits="W5_3_2_1" %><!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"><div><p>单价:<asp:TextBox ID="TextBox1" runat="server" Text="0" AutoPostBack="True" ontextchanged="TextBox1_TextChanged"></asp:TextBox></p><p>数量:<asp:TextBox ID="TextBox2" runat="server" Text="0" AutoPostBack="True" ontextchanged="TextBox2_TextChanged"></asp:TextBox></p><p></p><p><asp:Label ID="Label1" runat="server" Text="<%#Convert.ToString(Convert.ToDecimal(TextBox1.Text)*Convert.ToInt32(TextBox2.Text))%>"></asp:Label></p></div></form><script type="text/javascript"></script>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class W5_3_2_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){Page.DataBind();}protected void TextBox2_TextChanged(object sender, EventArgs e){TextBox1.DataBind();}protected void TextBox1_TextChanged(object sender, EventArgs e){TextBox2.DataBind();}
}

2.3  Button控件

Button控件可以分为提交按钮和命令按钮。

默认的Button按钮为提交按钮,在单击时,将包含它的表单提交给相应服务器进行处理,一般响应Click事件。

当设置了CommandName属性和CommandArgument属性后,Button按钮成为命令按钮,用于处理控件命令事件,在单击时可响应Command事件,从事件参数中可获取命令名及命令参数值。

例:响应Command事件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="W5_3_3_1.aspx.cs" Inherits="W5_3_3_1" %><!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><link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body><form id="form1" runat="server"><div class="container mx-auto text-center m-auto"><p><asp:Button ID="Button1" runat="server" Text="Button1" CommandArgument="button1" CommandName="B1" oncommand="Button1_Command" />&nbsp;<asp:Button ID="Button2" runat="server" Text="Button2" CommandArgument="button2" CommandName="B2" oncommand="Button2_Command" /></p><p><asp:Label ID="Label1" runat="server" Text="你点击的是:Button1" Enabled="False"></asp:Label></p><p><asp:Label ID="Label2" runat="server" Text="你点击的是:Button2" Enabled="False" ViewStateMode="Enabled"></asp:Label></p></div></form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class W5_3_3_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void Button1_Command(object sender, CommandEventArgs e){Label1.Visible = true;// Label1.Text = "你点击的是:" + e.CommandArgument.ToString();Label2.Visible = false;}protected void Button2_Command(object sender, CommandEventArgs e){Label2.Visible = true;// Label2.Text = "你点击的是:" + e.CommandArgument.ToString();Label1.Visible = false;}
}

3. 列表控件

3.1 ListBox

ListBox控件(列表框控件)用于显示一组列表项,用户可以从中选择一项或多项。

例1:实现选择按钮

w5_4_4_1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_4_1_1.aspx.cs" Inherits="w5_4_1_1" %><!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>listBox</title><style type="text/css">#div0{width:400px;height:200px;margin-left:auto;margin-right:auto;margin-top: 50px;margin:50px auto auto auto;}#div1{float:left;width:150px;height:200px;margin-right:10px;}#div2{float:left;width:80px;height:200px;text-align:center;margin-right:10px;}.btn{width:  50px;margin-top:10px;}</style>
</head>
<body><form id="form1" runat="server"><div><div id="div0"><div id="div1"><asp:ListBox ID="lstCouser" runat="server" Height="200px" Width="150px" SelectionMode="Multiple" onselectedindexchanged="lstCouser_SelectedIndexChanged"><asp:ListItem>动态网站设计</asp:ListItem><asp:ListItem>算法设计与分析</asp:ListItem><asp:ListItem>Java程序设计</asp:ListItem><asp:ListItem>数据结构</asp:ListItem></asp:ListBox></div><div id="div2"><asp:Button ID="btnSelectAll" runat="server" Text=">>" CssClass="btn" onclick="btnSelectAll_Click" /><asp:Button ID="btnRemoveAll" runat="server" Text="<<" CssClass="btn" onclick="btnRemoveAll_Click" /><asp:Button ID="btnSelect" runat="server" Text=">" CssClass="btn" onclick="btnSelect_Click" /><asp:Button ID="btnRemove" runat="server" Text="<" CssClass="btn" onclick="btnRemove_Click" /></div><div id="div3"><asp:ListBox ID="lstSelectdCourse" runat="server" Height="200px" Width="150px" SelectionMode="Multiple"></asp:ListBox></div></div></div></form>
</body>
</html>

W5_4_4_1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_4_1_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void lstCouser_SelectedIndexChanged(object sender, EventArgs e){}protected void btnSelectAll_Click(object sender, EventArgs e){int count = lstCouser.Items.Count;for (int i = 0; i < count; i++){ListItem item = lstCouser.Items[0];lstSelectdCourse.Items.Add(item);lstCouser.Items.Remove(item);}}protected void btnSelect_Click(object sender, EventArgs e){int count = lstCouser.Items.Count;int index = 0;for (int i = 0; i < count; i++){ListItem item = lstCouser.Items[index];if (item.Selected == true){lstSelectdCourse.Items.Add(item);lstCouser.Items.Remove(item);index--;}index++;}}protected void btnRemoveAll_Click(object sender, EventArgs e){int count = lstSelectdCourse.Items.Count;for (int i = 0; i < count; i++){ListItem item = lstSelectdCourse.Items[0];lstCouser.Items.Add(item);lstSelectdCourse.Items.Remove(item);}}protected void btnRemove_Click(object sender, EventArgs e){int count = lstSelectdCourse.Items.Count;int index = 0;for (int i = 0; i < count; i++){ListItem item = lstSelectdCourse.Items[index];if (item.Selected == true){lstCouser.Items.Add(item);lstSelectdCourse.Items.Remove(item);index--;}index++;}}
}

3.2 DropDownList控件

DropDownList控件(下拉列表框控件)让用户可以从单项选择下拉列表框中进行选择。

例子:

w5_4_2_1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_4_2_1.aspx.cs" Inherits="w5_4_2_1" %><!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"><div><p>爱好;<asp:DropDownList ID="ddlHobby" runat="server" AutoPostBack="True" onselectedindexchanged="ddlHobby_SelectedIndexChanged"></asp:DropDownList></p><p><asp:Label ID="lbl" runat="server" Text="请选择爱好!"></asp:Label></p></div></form>
</body>
</html>

w5_4_2_1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_4_2_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack) {List<string> List = new List<string> { "打球", "看书", "上网", "散步" };ddlHobby.DataSource = List;Page.DataBind();}}protected void ddlHobby_SelectedIndexChanged(object sender, EventArgs e){lbl.Text = "你的爱好是: " + ddlHobby.SelectedItem.Text;}
}

例二:

w5_4_2_2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_4_2_2.aspx.cs" Inherits="w5_4_2_2" %><!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>DropDownlist</title>
</head>
<body><form id="form1" runat="server"><div><p><asp:DropDownList ID="ddlDep" runat="server" AppendDataBoundItems="True" AutoPostBack="True"></asp:DropDownList></p><p><asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" /></p><p><asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label></p></div></form>
</body>
</html>

w5_4_2_2.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_4_2_2 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack) { List<Dep> deps = new List<Dep>();deps.Add(new Dep("d01", "中文系"));deps.Add(new Dep("d02", "外语系"));deps.Add(new Dep("d03", "数学系"));ddlDep.DataSource = deps;ddlDep.DataTextField = "Dname";ddlDep.DataValueField = "Dno";ddlDep.DataBind();ddlDep.Items.Insert(0,new ListItem("无",""));//添加空系}}protected void btnSubmit_Click(object sender, EventArgs e){lblMessage.Text = "你选中的值是:" + ddlDep.SelectedItem.Value+"<br />对应的文本是:" + ddlDep.SelectedItem.Text;}
}

3.3  CheckBoxList控件

CheckBoxList控件又称为复选框列表控件,该控件为用户提供了一种输入布尔型数据的方法,允许用户进行选择。
CheckBoxList控件与CheckBox控件类似,不同之处是CheckBox只有一个复选框,CheckBoxList包含多个复选框。

例:

w5_4_3_1..aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_4_3_1.aspx.cs" Inherits="w5_4_3_1" %><!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"><div><p>请选择你的爱好:</p><p> <asp:CheckBoxList ID="chkHobby" runat="server" AutoPostBack="True" CellSpacing="5" onselectedindexchanged="chkHobby_SelectedIndexChanged" RepeatColumns="2"><asp:ListItem>读书</asp:ListItem><asp:ListItem>绘画</asp:ListItem><asp:ListItem>游泳</asp:ListItem><asp:ListItem>音乐</asp:ListItem><asp:ListItem>摄影</asp:ListItem><asp:ListItem>跳舞</asp:ListItem></asp:CheckBoxList></p><p><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></p></div></form>
</body>
</html>

W5_4_3_1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_4_3_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void chkHobby_SelectedIndexChanged(object sender, EventArgs e){lblMessage.Text = "你的爱好:<br /><br />";for (int i = 0; i < chkHobby.Items.Count; i++) {if (chkHobby.Items[i].Selected) {lblMessage.Text += chkHobby.Items[i].Text + "<br />";}}}
}

3.4  RadioButtonList控件

RadioButtonList控件(单选按钮列表控件)用于构建单选按钮列表,允许用户互斥地在列表中选择一项。

例:

W5_4_4_1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_4_4_1.aspx.cs" Inherits="w5_4_4_1" %><!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><link href="css/bootstrap.css" rel="stylesheet" type="text/css" /><style type="text/css">table{  border-style :solid;border-width:1px 0px 0px 1px;width: 100%;}.td1{border-style: solid;border-width: 0px 1px 1px 0px;text-align: center;}.td2{border-style:solid;border-width: 0px 1px 1px 0px;}#div0{width:300px;height: 20px auto 0px auto ;}</style>
</head>
<body><form id="form1" runat="server"><div id="div0"><table cellspacing="0"><tr><td class="td1">性别:</td><td class="td2"> <asp:RadioButtonList ID="radlGender" runat="server" RepeatDirection="Horizontal" AutoPostBack="False"><asp:ListItem>男</asp:ListItem><asp:ListItem>女</asp:ListItem></asp:RadioButtonList></td></tr><tr><td class="td1">职称:</td><td class="td2"> <asp:RadioButtonList ID="radlTitle" runat="server" AutoPostBack="False"><asp:ListItem>助教</asp:ListItem><asp:ListItem>讲师</asp:ListItem><asp:ListItem>副教授</asp:ListItem><asp:ListItem>教授</asp:ListItem></asp:RadioButtonList></td></tr></table><p><asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" /></p><p> <asp:Label ID="lblmassage1" runat="server" Text=""></asp:Label><br /><asp:Label ID="lblmessage2" runat="server" Text=""></asp:Label></p></div></form>
</body>
</html>

W5_4_4_1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_4_4_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void btnSubmit_Click(object sender, EventArgs e){foreach (ListItem item in radlGender.Items) {if (item.Selected) {lblmassage1.Text = "性别为;" + item.Text;}}foreach (ListItem item in radlTitle.Items)if (item.Selected)lblmessage2.Text = "职称为;" + item.Text;}
}

bootstrap4实现

w5_4_4_2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_4_4_2.aspx.cs" Inherits="w5_4_4_2" %><!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>bs4实现</title><link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body><form id="form1" runat="server">
<div  class="container"><table class="table table-bordered w-25 h-25 table-striped" ><tr><td >性别:</td><td> <asp:RadioButtonList ID="radlGender" runat="server" RepeatDirection="Horizontal" AutoPostBack="False"><asp:ListItem>男</asp:ListItem><asp:ListItem>女</asp:ListItem></asp:RadioButtonList></td></tr><tr><td>职称:</td><td> <asp:RadioButtonList ID="radlTitle" runat="server" AutoPostBack="False"><asp:ListItem>助教</asp:ListItem><asp:ListItem>讲师</asp:ListItem><asp:ListItem>副教授</asp:ListItem><asp:ListItem>教授</asp:ListItem></asp:RadioButtonList></td></tr></table><p><asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" /></p><p> <asp:Label ID="lblmassage1" runat="server" Text=""></asp:Label><br /><asp:Label ID="lblmessage2" runat="server" Text=""></asp:Label></p></div></form>
</body>
</html>

w5_4_4_2.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_4_4_2 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void btnSubmit_Click(object sender, EventArgs e){}
}

3.5 验证控件

为了确保用户在表单的各个域中输入正确的数据或所输入的数据符合商业逻辑的需求,应用程序需要进行客户端和服务器端的一系列验证。
ASP.NET内置了一套用于进行验证的控件,使用这套控件,开发人员只需要定义几个属性或编写少量代码,就可以实现验证过程。

3.5.1 RequiredFieldValidator控件

RequiredFieldValidator控件要求用户必须在所关联的控件中输入一个值,不能为空。
常用属性说明:
①ControlToValidate,要进行检查的控件
②ErrorMessage,当检查不合法时,显示的错误信息
③Display,错误信息的显示方式。Static,错误信息在页面占有确定的位置;Dymatic,在错误信息出现时才占用页面空间;None,不出现错误信息,但可在ValidatorSummary控件中统一显示。
④ForeColor,错误信息文本的颜色

例:

w5_5_1_1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_5_1_1.aspx.cs" Inherits="w5_5_1_1" %><!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"><div><p> 姓名:<asp:TextBox ID="userName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*姓名不能为空" ControlToValidate="userName" ForeColor="Red"></asp:RequiredFieldValidator></p><p><asp:Button ID="btnSubmit" runat="server" Text="提交" /></p><p><asp:Label ID="lblMessage" runat="server" Text="还没提交"></asp:Label></p></div></form>
</body>
</html>

w5_5_1_1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_5_1_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){if (IsPostBack && (userName.Text != null)){lblMessage.Text = "已提交,";}}
}

3.5.2 CompareValidator控件

用于检查所关联的控件的值与其他值比较的结果,或进行一个数据类型的检查。
常用属性:
①Type,要比较的控件的数据类型,可以是String、Integer、Double、Date或Currency。
②Operator,比较操作,有七种比较方式,等于、不等、大于、大于等于、小于、小于等于及数据类型检查。
③ControlToCompare,与所验证的输入控件进行比较的输入控件
④ValueToCompare,与所验证的输入控件进行比较的常数值

例子:

w_5_2_1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_5_2_1.aspx.cs" Inherits="w5_5_2_1" %><!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"><div><p> 用户名:<asp:TextBox ID="userName" runat="server"></asp:TextBox></p><p>用户密码:<asp:TextBox ID="password1" runat="server" TextMode="Password"></asp:TextBox></p><p>确认密码:<asp:TextBox ID="password2" runat="server" TextMode="Password"></asp:TextBox><asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="*密码不一致" ControlToCompare="password1" ControlToValidate="password2" ForeColor="Red"></asp:CompareValidator></p><p>出生日期:<asp:TextBox ID="birthday" runat="server"></asp:TextBox><asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="*日期格式错误" ControlToValidate="birthday" ForeColor="Fuchsia" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator></p><p><asp:Button ID="btnSubmit" runat="server" Text="提交" /></p></div></form>
</body>
</html>

w5_5_2_1.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class w5_5_2_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}
}

3.5.3 RangeValidator控件

用于检查所关联控件的值是否在一个最小值和最大值之间。

3.5.4 RegularExpressionValidator控件

用于检查输入控件的值是否匹配正则表达式定义的模式。
如果输入控件为空,则表明验证成功。如果相关输入控件需要一个值,则除了使用 RegularExpressionValidator 控件外,还须使用 RequiredFieldValidator 控件。

正则表达式是正则表达式引擎尝试匹配输入文本的一种模式。模式由一个或多个字符文本、运算符或构造组成。
语法参见“正则表达式语言元素”。
https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/regular-expression-language-quick-reference

https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/regular-expression-language-quick-reference

https://www.cnblogs.com/xinaixia/p/4976821.html

https://www.cnblogs.com/testsec/p/6095656.html

https://www.cnblogs.com/eric_lin/archive/2010/11/11/1874749.html

https://www.cnblogs.com/shiguangshuo/p/4838845.html

https://blog.csdn.net/my98800/article/details/62214649

例子:

W5_5_4_1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="w5_5_4_1.aspx.cs" Inherits="w5_5_4_1" %><!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"><div><p> 电话:<asp:TextBox ID="txtTel" runat="server"></asp:TextBox></p><p> Email: <asp:TextBox ID="TxtEmail" runat="server"></asp:TextBox></p><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" validationexpression= "(13[0-9]{9})|(159[0-9]{8})|([0-9]{4}-[0-9]{8})|([0-9]{3}-[0-9]{8})|([0-9]{4}-[0-9]{7})"ErrorMessage=". 号码输入有误" ForeColor="Red" ControlToValidate="txtTel"></asp:RegularExpressionValidator><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*号码输入有误" ControlToValidate="txtTel"></asp:RequiredFieldValidator><br /><asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="*Email输入有误" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="TxtEmail"></asp:RegularExpressionValidator><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Email输入有误" ControlToValidate="TxtEmail"></asp:RequiredFieldValidator><p><asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" /></p><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></div></form>
</body>
</html>

aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;public partial class w5_5_4_1 : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){}protected void btnSubmit_Click(object sender, EventArgs e){/*Regex re = new Regex(@"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");//实例化一个Regex对象if (re.IsMatch(TxtEmail.Text) == true)//验证数据是否匹配{lblMessage.Text = "邮箱正确";//匹配则弹出”邮箱正确“}else{lblMessage.Text = "邮箱错误";//不匹配则弹出”邮箱错误“}Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$|^(13|15)\d{9}$", RegexOptions.None);*/}
}

C#网页设计 - Web控件相关推荐

  1. Html5table控件,Table Web 控件、TableRow Web 控件及TableCell Web 控件

    Table Web 控件的用法和传统的HTML 的Table 元素差异很大,为了将网页设计对象导向,Table内的列和字段也都跟着对象化了.前面我们已经提过HTML 控件的HtmlTable.Html ...

  2. 自定义服务器控件(扩展现有 Web 控件)

    很多情况下,你并不需要从头开始创建一个新控件.有些功能也许在 ASP.NET 的 Web 控件的基本集合中已经存在了.所有这些控件都是普通类,你可以组合它们(使用其他类的实例来创建一个新类)或者继承它 ...

  3. Html控件和Web控件的比较分析

     在ASP.NET中,使用了两类控件,一类是HTML控件(又分为HTML控件和HTML服务器控件),还有一类是WEB控件. 从以下几个方面来了解他们的区别和联系 一:概念 HTML控件:就是我们通 ...

  4. HTML控件和Web控件的区别和联系

    在ASP.NET中,使用了两类控件,一类是HTML控件(又分为HTML控件和HTML服务器控件),还有一类是WEB控件. 从以下几个方面来了解他们的区别和联系 一:概念 HTML控件:就是我们通常的说 ...

  5. 探讨ASP.NET 2.0中的Web控件改进技术(3)

    当你最开始在Visual Studio 2005中使用Windows表单控件或是ASP.NET Web控件时,你首先会注意到,在许多控件右上角出现一个箭头形状的小玩意儿(见图2中的示例).点击这个箭头 ...

  6. [原]动态创建Web控件制做计算器

    最近参加了Web基础开发的培训,收获不少,做了一个练习,在后台动态创建控件制作了一个简单功能的计算器.程序中控件创建好以后,往往不能放在想要的位置,前台的布局非常麻烦,我用Table.TableRow ...

  7. [MFC] MFC 打开HTML资源(用ID版,也可加载到自己的web控件上)

    @ ^ @:如果是加载到web控件上,就把注释掉的解除注释(改为web控件点后面的函数),把下一句注释 1 BOOL Button::LoadFromResource(UINT nRes){//打开网 ...

  8. 探讨ASP.NET2.0的Web控件改进之概述

    作者: 朱先忠编译 一. 引言 到目前为止,你可能已经了解了大量的ASP.NET 2.0新特征-母版页面,主题,提供者,等等--所有这样内容都相当精彩:但是,你是否了解到有关定制Web控件开发方面的重 ...

  9. [转]利用ASP.NET 2.0创建自定义Web控件(1)

    原址:http://hi.baidu.com/sjbh/blog/item/cc58fd1bd35d3ad2ad6e7593.html   简介 从使用基本的文本编辑器到创作标记页面,Web 开发已经 ...

最新文章

  1. java什么时会出现gc_面试题:java GC发生在会么时候,对什么东西,做了什么事情...
  2. C#中静态与非静态方法比较
  3. python中文字符编码问题
  4. java提高篇(十二)-----代码块
  5. CSS中的a标签几个访问状态记录
  6. oracle optimizer_features_enable,Oracle Optimizer:迁移到使用基于成本的优化器—–系列2.1-数据库专栏,ORACLE...
  7. vs2017 cmake android,CMake构建VS2017工程
  8. JavaScript 媒体查询库 enquire.js
  9. arma模型matlab代码_DCC GARCH模型
  10. array转list_Java面试题Array和ArrayList有何区别?
  11. 我使用的网址--Hadoop
  12. UE4官方文档阅读笔记——编程指南
  13. python扫雷总结与体会_扫雷项目总结
  14. 在Unity中模拟汽车的移动
  15. 8大成功的网络营销案例 互联网营销案例分析
  16. could not initialize javavm mysql_Could not initialize JavaVM
  17. ResNet详解(pytorch)
  18. CVE-2018-4878 flash漏洞复现
  19. 如何拦截烦人的视频广告
  20. 如何将PDF文件转换成TXT文档

热门文章

  1. hal库开启中断关中断_(2)STM32使用HAL库操作外部中断——理论讲解
  2. 云服务器标准方式登录Linux
  3. 在一台服务器上搭多个网站的解决方案
  4. TCRT5000 红外测距使用解析
  5. win7+ArcGIS10+注册机
  6. 听说这10道大数据面试题把 98% 的求职者拒之BAT大厂门外(附解题方法)
  7. Springboot 使用quartz 定时任务 增删改查
  8. 基于RFID的定位技术有几种?哪种最成熟?
  9. PB实现BASE64加解密
  10. 人脸识别-arcface损失函数