What is Checkbox Control and how to use it in ASP.NET
Hello guys! I hope you will be well there. Today in this article I will describe that What is checkbox control and how to use it in ASP.NET. So let’s first have a discuss that what is checkbox control and how to use it in ASP.NET, and then we will describe how to insert it in a web form and their properties. So we start now…
What is checkbox control
Checkbox control is a standard control which resides in toolbox menu in Visual Studio (VS) Framework. Checkbox control is used to select any single value like I Agree checkbox in any web form. Checkbox returns true or false value depending on its selected state. If checkbox is selected then it will return true value else it will return false value.
How to insert checkbox control in web form
To insert checkbox control in any web form just click on toolbox in VS Framework and then either double click on checkbox control or drag it in your web form to insert it. When you insert checkbox control in your web form then following coding shows automatically.
<asp:CheckBox ID=”CheckBox1″ runat=”server” />
In above coding you may change the value of ID to any value you want. Now the next step is give a caption value to this checkbox control. For this we use the Text Property of checkbox control. Suppose you want to give a caption value I Agree all terms and conditions , then update the above coding with the following…
<asp:CheckBox ID=”CheckBox1″ runat=”server” Text=”I Agree all terms and conditions” />
Now if you run the program then you will see a checkbox with caption “ I agree all terms and conditions”. So this is the basic concept of checkbox control in asp.net. Now we will discuss some more properties of this control.
Properties of checkbox control
Properties related to Appearance
Checkbox control has some basic properties related to its appearance like ForeColor, BackColor, BorderColor, Font etc. You may give any value to these value either in source page by manual typing the following coding or you may switch to design page and then right click on the control, then click on properties. You will see the properties panel, from there you may set any value you want and I would like to reveal here that my preferred method is from design mode. Just consider the following coding and then watch the following video to understand this…
<asp:CheckBox ID="CheckBox1" runat="server"
Text="I Agree all terms and conditions" BorderColor="#FFFFCC"
BorderStyle="Dashed" ForeColor="#FF9966" oncheckedchanged="My_Event" />
When this property is set to true then on clicking (check or uncheck) the checkbox control, page postback to server. Postback to server means that the request goes to server to reload the page. This property is set to true when we require to perform any task on clicking the checkbox control and for this we can use Checkedchanged Event to handle this task.
Checkedchanged event is used to handle any task when someone clicks on the checkbox. To create checkedchanged event just go to the property of the checkbox and click in the checkedchanged textbox and press enter. On pressing enter an event with the default name will be generated. If you want to give any name to this event then type the name of the event and then press enter. Just watch the following video to understand this….
Remember one thing: checkedchanged event only works when you set autopostback property to true.
In this article I have discussed most common and basic features of checkbox control. In our next article I will discuss that how to use checkedchanged event and how can we use its other properties in programming. That’s all for now… happy coding….