Wednesday, 13 September 2017

How To Create Web Forms CheckBox In ASP.NET

How To Create Web Forms CheckBox In ASP.NET

Example
 WebControls.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs"  
Inherits="WebFormsControlls.WebControls" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
        <div> 
            <h2>Select Courses</h2> 
            <asp:CheckBox ID="CheckBox1" runat="server" Text="J2SE" /> 
            <asp:CheckBox ID="CheckBox2" runat="server" Text="J2EE" /> 
            <asp:CheckBox ID="CheckBox3" runat="server" Text="Spring" /> 
        </div> 
        <p> 
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
        </p> 
    </form> 
    <p> 
        Courses Selected: <asp:Label runat="server" ID="ShowCourses"></asp:Label> 
    </p> 
</body> 
</html> 

Code Behind
 

 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; 
        } 
    } 
} 

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search