Parag’s Den

Dedicated to my adored friends…..

CheckBox Web Server Control

Posted by paragdongre on November 6, 2007


Checkbox web server control is equivalent to the <input type=checkbox> tag in HTML. This control can be used to get multiple answers for any given question. In this session, we will see, how can we get the hobbies of any user. We will see the following aspects.

  1. How to assign a text to Checkbox control?
  2. How to know whether the checkbox has been checked or not?
  3. How to assign a value to Checkbox control?
  4. How to add an attribute to a CheckBox Control?
  5. How to retrieve an attribute from a CheckBox Control?
Checkbox – How to assign a text to Checkbox control and how to know the checkboxes that are selected?

Creating a checkbox is very easy indeed. The following line creates a Checkbox control. <asp:Checkbox Runat=Server />. To assign a text to this checkbox control, we can either do it in the declaration itself, or we can generate the text from server side itself. To assign a text while creating, all you have to do is to use the property text. The following example depicts how to create checkbox web server control and how to assign values from the server side.

<HTML>
<HEAD>
<title>Checkbox Web Server Control – by Das </title>

<script language=vb runat=server>

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     ‘Put user code to initialize the page here
     chkMusic.Text = “Music”
     chkDriving.Text = “Driving”
     chkFishing.Text = “Fishing”
     chkBoating.Text = “Boating”
     chkEating.Text = “Eating”
End Sub

Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
     Dim strResult As String = “So, you like <br>”

If chkMusic.Checked Then
     strResult += chkMusic.Text & “, “
End If

If chkDriving.Checked Then
     strResult += chkDriving.Text & “, “
End If

If chkFishing.Checked Then
     strResult += chkFishing.Text & “, “
End If

If chkBoating.Checked Then
     strResult += chkBoating.Text & “, “
End If

If chkEating.Checked Then
     strResult += chkEating.Text
End If

     lblResult.Text = strResult & ” right?”
End Sub

</script>
</head>
<body>

<form id=”Form1″ method=”post” runat=”server”>

     <asp:Label ID=lblHobby text=”What are your hobbies?” Runat=server />
     <asp:CheckBox ID=”chkMusic” Runat=server />
     <asp:CheckBox ID=”chkDriving” Runat=server />
     <asp:CheckBox ID=”chkFishing” Runat=server />
     <asp:CheckBox ID=”chkBoating” Runat=server />
     <asp:CheckBox ID=”chkEating” Runat=server />
     <asp:Button ID=”btnSubmit” Text=”Submit” OnClick=”btnSubmit_Click” Runat=server />
     <asp:Label ID=”lblResult” ForeColor=”#ff0000″ Font-Bold=”True” Runat=server />

</form>

</body>
</HTML>

The above example is very simple. There are two things to learn from the above example. First, to assign a text to the textbox web server control, you have to use the property text. And to know whether the checkbox has selected or not, you have to use the property checked.

Checkbox – How to assign a Value to the Checkbox control?

By default, checkbox control does not support the property value. In classic ASP, we used this property to assign any value to the checkbox control. So, in ASP .NET, how to assign a value to the checkbox control. We have to make use of the Attributes method. We have to add an attribute to every checkbox web control. The following line, adds an attribute to the checkbox, Music. chkMusic.Attributes.Add(“Value”, 1). So, the method add takes two parameters. The first parameter is the attribute name. And the second parameter is the Value for this attribute. In the above example, we have created an attribute called Value. The following example exaplains how to assign attributes and how to retrieve them.

<HTML>
<HEAD>
<title>Checkbox Web Server Control – by Das </title>

<script language=vb runat=server>

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     ‘Put user code to initialize the page here
     chkMusic.Text = “Music”
     chkDriving.Text = “Driving”
     chkFishing.Text = “Fishing”
     chkBoating.Text = “Boating”
     chkEating.Text = “Eating”

     chkMusic.Attributes.Add(“Value”, 1)
     chkDriving.Attributes.Add(“Value”, 2)
     chkFishing.Attributes.Add(“Value”, 3)
     chkBoating.Attributes.Add(“Value”, 4)
     chkEating.Attributes.Add(“Value”, 5)
End Sub

Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
     Dim strResult As String = “Values for your selections are <br>”

If chkMusic.Checked Then
     strResult += chkMusic.Attributes(“Value”) & “, “
End If

If chkDriving.Checked Then
     strResult += chkDriving.Attributes(“Value”) & “, “
End If

If chkFishing.Checked Then
     strResult += chkFishing.Attributes(“Value”) & “, “
End If

If chkBoating.Checked Then
     strResult += chkBoating.Attributes(“Value”) & “, “
End If

If chkEating.Checked Then
     strResult += chkEating.Attributes(“Value”)
End If

     lblResult.Text = strResult
End Sub

</script>
</head>
<body>

<form id=”Form1″ method=”post” runat=”server”>

     <asp:Label ID=lblHobby text=”What are your hobbies?” Runat=server />
     <asp:CheckBox ID=”chkMusic” Runat=server />
     <asp:CheckBox ID=”chkDriving” Runat=server />
     <asp:CheckBox ID=”chkFishing” Runat=server />
     <asp:CheckBox ID=”chkBoating” Runat=server />
     <asp:CheckBox ID=”chkEating” Runat=server />
     <asp:Button ID=”btnSubmit” Text=”Submit” OnClick=”btnSubmit_Click” Runat=server />
     <asp:Label ID=”lblResult” ForeColor=”#ff0000″ Font-Bold=”True” Runat=server />

</form>

</body>
</HTML>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: