当前位置:Gxlcms > asp.net > ASP.NET中 CheckBox复选框控件的使用

ASP.NET中 CheckBox复选框控件的使用

时间:2021-07-01 10:21:17 帮助过:12人阅读

我们可以使用两种类型的 ASP.NET 控件将复选框添加到 Web 窗体页上:单独的 CheckBox 控件或 CheckBoxList 控件。两种控件都为用户提供了一种输入布尔型数据(真或假、是或否)的方法。

这里我们单独使用CheckBox,先来看看它的属性

属性 描述 .NET
AutoPostBack 规定在 Checked 属性已改变后,是否立即向服务器回传表单。默认是 false。 1.0
CausesValidation 规定点击 Button 控件时是否执行验证。 2.0
Checked 规定是否已选中该复选框。 1.0
InputAttributes 该 CheckBox 控件的 Input 元素所用的属性名和值的集合。 2.0
LabelAttributes 该 CheckBox 控件的 Label 元素所用的属性名和值的集合。 2.0
runat 规定该控件是服务器控件。必须被设置为 "server"。 1.0
Text 与复选框关联的文本标签。 1.0
TextAlign 与复选框关联的文本标签的对齐方式。(right 或 left) 1.0
ValidationGroup 在 CheckBox 控件回发到服务器时要进行验证的控件组。 2.0
OnCheckedChanged 当 Checked 属性被改变时,被执行函数的名称。

让我们来做个简单的示例来演示一下

前台代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBox.aspx.cs" Inherits="WebControls_CheckBox" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. width: 107px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div>
  16. <h3>CheckBox(复选框)</h3>
  17. <table style="width: 100%;">
  18. <tr>
  19. <td class="style1">
  20.  
  21. 属性</td>
  22. <td>
  23. 值</td>
  24. <td>
  25. 作用</td>
  26. </tr>
  27. <tr>
  28. <td class="style1">
  29.  
  30. Checked</td>
  31. <td>
  32.  
  33. ture|false</td>
  34. <td>
  35.  选中状态|未选状态
  36. </td>
  37. </tr>
  38. </table>
  39. <hr />
  40. 请选择你喜欢的运动:
  41. <asp:CheckBox ID="chkSport" runat="server" Text="篮球" Checked="true" />
  42. <asp:CheckBox ID="chkSport2" runat="server" Text="足球" />
  43. <asp:CheckBox ID="chkSport3" runat="server" Text="地瓜" />
  44. <br />
  45. <asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" />
  46. <hr />
  47. 你选择的爱好是:<asp:Label ID="lblState" runat="server"></asp:Label>
  48. </div>
  49. </form>
  50. </body>
  51. </html>

后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. public partial class WebControls_CheckBox : System.Web.UI.Page
  7. {
  8. protected void Page_Load(object sender, EventArgs e)
  9. {
  10. }
  11. protected void btnSubmit_Click(object sender, EventArgs e)
  12. {
  13. lblState.Text = string.Empty;
  14. if (chkSport.Checked)
  15. {
  16. lblState.Text = lblState.Text + chkSport.Text;
  17. }
  18. if (chkSport2.Checked)
  19. {
  20. if (lblState.Text.Length == 0)
  21. {
  22. lblState.Text = chkSport2.Text;
  23. }
  24. else
  25. {
  26. lblState.Text = lblState.Text + "," + chkSport2.Text;
  27. }
  28. }
  29. if (chkSport3.Checked)
  30. {
  31. if (lblState.Text.Length == 0)
  32. {
  33. lblState.Text = chkSport2.Text;
  34. }
  35. else
  36. {
  37. lblState.Text = lblState.Text + "," + chkSport3.Text;
  38. }
  39. }
  40. }
  41. }

运行效果:

CheckBox复选框控件

人气教程排行