📜  ASP.Net复选框

📅  最后修改于: 2020-12-27 13:32:34             🧑  作者: Mango

ASP.NET Web窗体复选框

它用于从用户获取多个输入。它允许用户从选项集中选择选项。

它接受用户输入的是或否格式。当我们希望用户选择多个选项时,这很有用。

要创建CheckBox,我们可以将其从Visual Studio的工具箱中拖动。

这是一个服务器端控件,ASP.NET提供了自己的标签来创建它。下面给出示例。

< asp:CheckBox ID="CheckBox2" runat="server" Text="J2EE"/>

服务器将其呈现为HTML控件,并向浏览器生成以下代码。

< input id="CheckBox2" type="checkbox" name="CheckBox2" />

该控件具有自己的属性,如下表所示。

Property Description
AccessKey It is used to set keyboard shortcut for the control.
TabIndex The tab order of the control.
BackColor It is used to set background color of the control.
BorderColor It is used to set border color of the control.
BorderWidth It is used to set width of border of the control.
Font It is used to set font for the control text.
ForeColor It is used to set color of the control text.
Text It is used to set text to be shown for the control.
ToolTip It displays the text when mouse is over the control.
Visible To set visibility of control on the form.
Height It is used to set height of the control.
Width It is used to set width of the control.
Checked It is used to set check state of the control either true or false.

// WebControls.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs" 
Inherits="WebFormsControlls.WebControls" %>



    


    

Select Courses

Courses Selected:

背后的代码

// WebControls.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
    public partial class WebControls : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowCourses.Text = "None";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            var message = "" ;
            if (CheckBox1.Checked)
            {
                message = CheckBox1.Text+" ";
            }
            if (CheckBox2.Checked)
            {
                message += CheckBox2.Text + " ";
            }
            if (CheckBox3.Checked)
            {
                message += CheckBox3.Text;
            }
            ShowCourses.Text = message;
        }
    }
}

最初,没有课程选择,然后没有显示。它显示用户选择,如以下屏幕截图所示。