日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

【ASP.NET】服务器控件大演练与实例分析

發(fā)布時(shí)間:2024/4/15 asp.net 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ASP.NET】服务器控件大演练与实例分析 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

asp服務(wù)器控件

簡(jiǎn)介(特點(diǎn)):保存視圖狀態(tài),公共對(duì)象模型,簡(jiǎn)單,用戶定制,創(chuàng)建瀏覽器特定HTML

?

  • ?web服務(wù)器控件:文本控件(2個(gè))
  • ??? 1)?TextBox控件

    <asp:TextBox ID="TextBox5" runat="server" AutoPostBack="True"?? BackColor="#CCCCCC" BorderColor="#FF0080" BorderStyle="Solid" BorderWidth="3px"? Font-Bold="True" Font-Overline="True" Font-Size="Larger" Font-Strikeout="False"? ForeColor="Red" Height="79px" MaxLength="12"? ontextchanged="TextBox5_TextChanged" ReadOnly="True" TextMode="MultiLine" ToolTip="這是一個(gè)文本控件" ViewStateMode="Enabled" Width="282px">文本控件屬性事件</asp:TextBox>
    ??????2) Label控件

    • 控制權(quán)轉(zhuǎn)移控件(4個(gè))1)Button 2)ImageButton3)LinkButton4)HyperLink

    2?? 選擇控件,(4)

    1)DropDownList控件

    代碼如下:

    View Code 1 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 dlbl.Text = "你選擇的是" +DropDownList1.SelectedItem.Value +"."; 4 }

    ?


    2)CheckBoxList控件

    代碼如下:

    View Code 1 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 string str = ""; 4 foreach (ListItem item in CheckBoxList1.Items) 5 { 6 if (item.Selected) 7 { 8 str+=item.Value+""; 9 } 10 } 11 chlbl.Text = "你選擇的是:" + str + "."; 12 }

    ?


    3)RadioButtonList控件

    代碼如下:

    View Code 1 protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 string str = ""; 4 foreach (ListItem item in RadioButtonList1.Items) 5 { 6 if (item.Selected) 7 { 8 str = item.Value + ""; 9 } 10 } 11 rlbl.Text = "你選擇的是:" + str + "."; 12 }

    ?


    4)ListBox控件

    View Code <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" onselectedindexchanged="ListBox1_SelectedIndexChanged" SelectionMode="Multiple"><asp:ListItem>唱歌</asp:ListItem><asp:ListItem>寫(xiě)代碼</asp:ListItem><asp:ListItem>游戲</asp:ListItem></asp:ListBox><br /><br /><asp:Label ID="lbrlbl" runat="server" /> View Code 1 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 string str = ""; 4 foreach (ListItem item in ListBox1.Items) 5 { 6 if (item.Selected) 7 { 8 str += item.Value + ""; 9 } 10 } 11 lbrlbl.Text = "你選擇的是:" + str + "."; 12 }

    ?

    3? 容器控件

    1)Panel控件

    panel.aspx:

    View Code <asp:Panel ID="Panel1" runat="server" BorderColor="Blue" BorderStyle="Solid" BorderWidth="4px" Height="45px" οnlοad="Panel1_Load" Width="543px"></asp:Panel>

    panel.aspx.cs

    View Code 1 protected void Panel1_Load(object sender, EventArgs e) 2 { 3 TextBox text = new TextBox(); 4 text.BorderColor = System.Drawing.Color.Red; 5 text.BorderStyle = BorderStyle.Solid; 6 text.BorderWidth = 2; 7 text.Text = "this is panel test page."; 8 this.Panel1.Controls.Add(text); 9 }

    ?


    2)PlaceHolder控件

    placeholder.aspx:

    View Code <asp:PlaceHolder ID="PlaceHolder1" runat="server" οnlοad="PlaceHolder1_Load"></asp:PlaceHolder>

    ?

    placeholder.aspx.cs:

    View Code 1 protected void PlaceHolder1_Load(object sender, EventArgs e) 2 { 3 TextBox text = new TextBox(); 4 text.BorderColor = System.Drawing.Color.Red; 5 text.BorderStyle = BorderStyle.Solid; 6 text.BorderWidth = 2; 7 text.Text = "this is panel test page."; 8 this.PlaceHolder1.Controls.Add(text); 9 }

    ? 4 ?HTML服務(wù)器控件:

    • HtmlFrom控件,

    ?<form id="form1" runat="server" method="post" action="目標(biāo)頁(yè)面的URL"></form>

    5 HtmlImage控件

    <img id="img1" src="圖像URL" runat="server"/>

    ? 6 驗(yàn)證控件:

    通過(guò)一個(gè)實(shí)例驗(yàn)證所有控件

    ?

    前臺(tái)代碼:

    View Code 1 <asp:Panel ID="Panel2" runat="server" Height="382px" style=" margin-left:300px" Width="440px" BackColor="#99CCFF" BorderColor="Red" BorderWidth="4px"> 2 <div id="div1"> 3 <ul style="font-size:small; vertical-align:middle;"> 4 <li style=" font-size:large; text-align: center;">用戶注冊(cè)</li> 5 <li>用戶名:&nbsp;&nbsp;<asp:TextBox ID="txtname" runat="server"/> 6 <asp:RequiredFieldValidator ID="rfvname" runat="server" ErrorMessage="用戶名不能為空" ForeColor="red" ControlToValidate="txtname"></asp:RequiredFieldValidator> 7 <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtname" ForeColor="Red" ErrorMessage="必須包含a" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator> 8 </li> 9 <li>密 碼:&nbsp;&nbsp; <asp:TextBox ID="txtpass" runat="server"/> 10 <asp:RequiredFieldValidator ID="rfvpwd" runat="server" ControlToValidate="txtpass" ForeColor="red" ErrorMessage="密碼不能為空"></asp:RequiredFieldValidator> 11 </li> 12 <li>確認(rèn)密碼:&nbsp; <asp:TextBox ID="txtrepass" runat="server"/> 13 <asp:CompareValidator ID="cvpwd" runat="server" ControlToCompare="txtpass" ForeColor="red" ControlToValidate="txtrepass" ErrorMessage="確認(rèn)密碼和密碼必須相同"></asp:CompareValidator> 14 </li> 15 <li> 16 出生日期:&nbsp;&nbsp; <asp:TextBox ID="txtbirthday" runat="server" /> 17 <asp:CompareValidator ID="cvbri" runat="server" ControlToValidate="txtbirthday" ForeColor="red" ErrorMessage="輸入必須是日期" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator> 18 <asp:RangeValidator ID="rvBrithday" runat="server" ControlToValidate="txtbirthday" MaximumValue="2012/12/31" MinimumValue="2012/1/1" ForeColor="Red" ErrorMessage="日期超出范圍"></asp:RangeValidator> 19 </li> 20 <li> 21 郵箱:&nbsp;&nbsp;&nbsp; <asp:TextBox ID="txtem" runat="server" /> 22 <asp:RegularExpressionValidator ID="revEmail" runat="server" ErrorMessage="請(qǐng)輸入正確的E-mail地址!" ControlToValidate="txtem" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> 23 </li> 24 <li>性別: 25 <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal"> 26 <asp:ListItem Selected="True">男</asp:ListItem> 27 <asp:ListItem>女</asp:ListItem> 28 </asp:RadioButtonList> 29 </li> 30 <li> 31 愛(ài)好: 32 <asp:CheckBoxList ID="CheckBoxList2" runat="server" RepeatDirection="Horizontal"> 33 <asp:ListItem>睡覺(jué)</asp:ListItem> 34 <asp:ListItem>聊天</asp:ListItem> 35 <asp:ListItem>寫(xiě)代碼</asp:ListItem> 36 </asp:CheckBoxList> 37 </li> 38 <li> 39 省份: 40 <asp:DropDownList ID="DropDownList2" runat="server"> 41 <asp:ListItem >河南省</asp:ListItem> 42 <asp:ListItem >安徽省</asp:ListItem> 43 <asp:ListItem >河北省</asp:ListItem> 44 </asp:DropDownList> 45 </li> 46 <li> 47 <asp:Button ID="Button4" runat="server" Text="注冊(cè)" οnclick="Button4_Click" /> 48 <asp:Button ID="Button5" runat="server" Text="取消" /> 49 </li> 50 <li> 51 <asp:TextBox TextMode="MultiLine" ID="zhucetxt" runat="server" Height="44px" Width="378px"></asp:TextBox> 52 </li> 53 <li> 54 <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> 55 </li> 56 </ul> 57 </div> 58 </asp:Panel>

    后臺(tái)代碼:

    View Code 1 protected void Button4_Click(object sender, EventArgs e) 2 { 3 string name = txtname.Text; 4 string pass = txtpass.Text; 5 string repass = txtrepass.Text; 6 string birthday = txtbirthday.Text; 7 if (repass != pass) 8 { 9 Response.Write("<script>alert('密碼必須相同!')</script>"); 10 } 11 string sex=RadioButtonList2.SelectedItem.Value; 12 string str = ""; 13 foreach(ListItem item in CheckBoxList2.Items) 14 { 15 if(item.Selected) 16 { 17 str+=item.Value; 18 } 19 } 20 string like = str; 21 string pro = DropDownList2.SelectedItem.Value; 22 zhucetxt.Text = "姓名:" + name + ""+ "密碼:" + pass + "" +"生日:"+birthday + "性別:" + sex + ""+ "愛(ài)好:" + like + ""+ "省份:" + pro; 23 }

    ??1)RequiredFiledValidator控件:用于驗(yàn)證必填字段中是否輸入了值。

    用戶名:&nbsp;&nbsp;<asp:TextBox? ID="txtname" runat="server"/>
    <asp:RequiredFieldValidator ID="rfvname" runat="server" ErrorMessage="用戶名不能為空" ForeColor="red"ControlToValidate="txtname"/>???????????????

    <asp:CustomValidator ID="CustomValidator1" runat="server"? ControlToValidate="txtname" ForeColor="Red" ErrorMessage="必須包含a" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>?????????????

    ?2)CompareValidator控件:將用戶輸入的值和另一個(gè)控件值進(jìn)行比較,或固定值比較。

    確認(rèn)密碼:&nbsp; <asp:TextBox ID="txtrepass" runat="server"/>

    ?<asp:CompareValidator ID="cvpwd" runat="server" ControlToCompare="txtpass"ForeColor="red"ControlToValidate="txtrepass"?? ErrorMessage="確認(rèn)密碼和密碼必須相同"/>???????

    3)RangeValidator控件:驗(yàn)證用戶輸入的值是否在指定范圍

    ? 出生日期:&nbsp;&nbsp; <asp:TextBox ID="txtbirthday" runat="server" />
    ?<asp:CompareValidator ID="cvbri" runat="server"? ControlToValidate="txtbirthday" ForeColor="red" ErrorMessage="輸入必須是日期" Operator="DataTypeCheck" Type="Date"/>?

    <asp:RangeValidator ID="rvBrithday" runat="server" ControlToValidate="txtbirthday" MaximumValue="2012/12/31" MinimumValue="2012/1/1" ForeColor="Red" ErrorMessage="日期超出范圍"/>

    4)RegularExpressionValidator控件:驗(yàn)證輸入的值是否與正則表達(dá)式匹配

    ?郵箱:&nbsp;&nbsp;&nbsp; <asp:TextBox ID="txtem" runat="server" />
    ?<asp:RegularExpressionValidator ID="revEmail" runat="server"? ErrorMessage="請(qǐng)輸入正確的E-mail地址!" ControlToValidate="txtem" ForeColor="Red"? ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

    5)CustomValidator控件:用于創(chuàng)建自定義驗(yàn)證?????

    用戶名???? <asp:TextBox? ID="txtname" runat="server"/>
    ?<asp:CustomValidator ID="CustomValidator1" runat="server"? ControlToValidate="txtname" ForeColor="Red" ErrorMessage="必須包含? ?a" onservervalidate="CustomValidator1_ServerValidate"/>????

    CustomValidator1_ServerValidate事件:

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    ??????? {
    ??????????? args.IsValid = args.Value.Contains("a");
    ??????? }

    6)ValidationSummar控件:用于概括所有錯(cuò)誤提示

    ? <asp:ValidationSummary ID="ValidationSummary1" runat="server" />

    ==》滿足所有條件,運(yùn)行正確結(jié)果:

  • ?自定義控件:
  • 常見(jiàn)自定義服務(wù)器控件分4種:復(fù)合控件,驗(yàn)證控件,模板控件和數(shù)據(jù)綁定控件

    ?

    ?

    總結(jié)

    以上是生活随笔為你收集整理的【ASP.NET】服务器控件大演练与实例分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。