Tuesday, 16 July 2013

CheckBox example in Asp.net

Aspdotnet provides different kind of standard controls.Here i would like to explain about one of the asp.net server control which is CheckBox. This control can take input as Boolean values.The Boolean value is true when check box checked and its false when it is unchecked.The post-back will also do using the check-box control when this property set to true
In the below example i have changing the color of label ble control when it was checked.For this we have to add system.Drawing namespace to our application
CheckBox.Aspx:
<html>
<head>
<title>CheckBox Example in Asp.net</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:CheckBox id="Chk" Text="Name" runat="server" />
<asp:button ID="btn" text="Check" runat="server" onclick="btn_Click"/>
<asp:Label id="lblName" runat="server" />
</form>
</body>
</html>
CodeBehind:
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;

public partial class Checkbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
if(Chk.Checked==true)
{
    Color Green = Color.FromName("Green");
    lblName.BackColor = Green;
    lblName.Text = "CheckBox checked";

}else
{
    Color Red = Color.FromName("Red");
    lblName.BackColor = Red;
    lblName.Text = "CheckBox Not checked";
}
}
}

No comments:

Post a Comment