CheckBox Selection Limit in ASP.NET

By | July 17, 2012

CheckBox control is the very common control of HTML, unlike radio buttons it can select multiple items on a webpage. CheckBox control in ASP.NET has many properties some of them are listed below.

Property Description
AutoPostBack Specifies whether the form should be posted immediately after the Checked property has changed or not. Default is false
CausesValidation Specifies if a page is validated when a Button control is clicked
Checked Specifies whether the check box is checked or not
InputAttributes Attribute names and values used for the Input element for the CheckBox control
LabelAttributes Attribute names and values used for the Label element for the CheckBox control
runat Specifies that the control is a server control.  Must be set to “server”
Text The text next to the check box
TextAlign On which side of the check box the text should appear (right or left)
ValidationGroup Group of controls for which the Checkbox control causes validation when it posts back to the server
OnCheckedChanged The name of the function to be executed when the Checked property has changed

In this article I will teach you how to limit the number of selected items among all the checkbox on a webpage. First step, create a “ASP.NET Empty Web Application“.

As we have started with an Empty Web Application we need to add a page now. Goto Project>Add New Item (Ctrl+Shift+A), a screen will popup add a web form from it name it ‘SelectionLimit.aspx’.

The explorer will have SelectionLimit.aspx open for you with some default code, add a Check Box List either from toolbox or writing the code. Since we need to limit the number of selections we need to call a function or submit our form every time a selection is made in check box list, for this we will use the property AutoPostBack and set this to “true” we need to call a function on every Post Back so we will trigger an event on selection changed. To do this go to properties on right bottom corner.

As soon as you double click the SelectedIndexChanged, “SelectionLimit.aspx.cs” will show up where you will write code.

we will have two functions there

protected void Page_Load(object sender, EventArgs e), and

protected void MyCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)

since we have just added CheckBoxList and not added any items to it, so in the ‘Page_Load’ Function we will add items to CheckBoxList

and in the ‘MyCheckBoxList_SelectedIndexChanged’ function we will do the main coding part.

That is all, debug your page, u might have noticed in the ‘if’ condition I have used count > 2 i.e  I’m limiting the selection to two selections only i.e. only two items can be selected.

Source Code here.

 

One thought on “CheckBox Selection Limit in ASP.NET

  1. Jones

    Well I am adding this RSS to my e-mail and could look out for much more of your respective interesting content. Make sure you update this again very soon..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *