The CheckBox Control & The CheckBoxList Control

The CheckBox control is used to get a YES or NO answer from your user. Use the checkbox control when you only want to know the answer to a specific question. Use the checkboxlist control when you want to group the available options into categories.  Once you get the answer to your question by checking the .checked in your VB.NET or C# code then you can perform an action based on the answer.

A single checkbox control with AutoPostBack = true

When you want to perform a response immediately after a user clicks a checkbox (usually only used if using a single checkbox then in your checkbox control select AutoPostback ="true". But most of the time this is not something you want to do because other forms on the field need to be completed and the entire form data can be captured in a button click event. You will see in this example how the visible property can be very useful when you only want to display controls based on other events.

 

 


 
 

 

ASP.NET

    
<asp:CheckBox ID="ckSales" runat="server" Text=" I would like a salesperson 
to call me" AutoPostBack="True" />    
    

 

VB.NET

    
 Protected Sub ckSales_CheckedChanged(ByVal sender As Object, ByVal e As 
      System.EventArgs) Handles ckSales.CheckedChanged 

   If ckSales.Checked Then 
       txtNeedName.Visible = True 
       txtPhone.Visible = True 
       btnCallMe.Visible = True 
   Else 
       txtNeedName.Visible = False 
       txtPhone.Visible = False 
       btnCallMe.Visible = False 
       lblConfirm.Visible = False 
   End If 
End Sub    
 
 
 
   Protected Sub btnCallMe_Click(ByVal sender As Object, ByVal e As 
         System.EventArgs) Handles btnCallMe.Click 

    If ckSales.Checked Then 
        txtNeedName.Visible = False 
        txtPhone.Visible = False 
        btnCallMe.Visible = False 
        lblConfirm.Visible = True 
        lblConfirm.Text = "Okay, " + txtNeedName.Text + ", a salesperson will  
            call you at " + txtPhone.Text + " within 24 hours." 

    Else 
       txtNeedName.Visible = False 
       btnCallMe.Visible = False 
       lblConfirm.Visible = False 
    End If 

      

CheckBoxList

Use a checkboxlist control when you have several questions that can be grouped into one category.
In this scenerio you would write code to see if each box was checked by creating an "if/else" statement as shown in the example further down the page. If you want an immediate response for each checked box set the property EnablePostBack=True. Otherwise, you will need to add a button control and add your code there.

EXAMPLE FORM:

I am interested in the following:

 




When you need to supply a dynamic list you can "bind" your control to a DataSource.