CheckBox ASP.NET Server Control

CheckBox ASP.NET Server Control"-->

only for RuBoard

CheckBox ASP.NET Server Control

There are two types of CheckBox server controls: CheckBox and CheckBoxList . The Checkbox control provides a way for users to input Boolean data, either a true or false value. For example, you could provide a CheckBox on your site so users could indicate whether they would like to receive a weekly newsletter. If they would not like to receive it, they wouldn't check the box.

One attribute with which you should be familiar when using the CheckBox control is the Checked attribute. The Checked attribute expects a Boolean value ( true or false ) and if true the CheckBox will be selected; if false then it is not. Listing 8.16 demonstrates the use of the CheckBox control ”the top CheckBox has Checked set to true and the bottom is false .

Listing 8.16 The CheckBox Control
 01: <html> 02:  <body> 03:   <form runat="server"> 04: 05:    <asp:Checkbox runat="server" 06:     id="cb" 07:     Checked="true" 08:     Text="Check Me Out" 09:    /> 10: 11:    <br> 12: 13:    <asp:Checkbox runat="server" 14:     id="cb2" 15:     Checked="false" 16:     Text="Check Me Out" 17:    /> 18: 19:   </form> 20:  </body> 21: </html> 

When the page from Listing 8.16 is rendered you'll get a page with two CheckBox controls, both with the text "Check Me Out" to the right. The top CheckBox is checked when it first loads because I set the Checked attribute to true ”the bottom is set to false so it will not be checked. The following HTML is what is actually rendered to the client:

 01: <input id="cb" type="checkbox" name="cb" checked="checked" /><label for="cb">Check Me graphics/ccc.gif Out</label> 02: 03: <br> 04: 05: <input id="cb2" type="checkbox" name="cb2" /><label for="cb2">Check Me Out</label> 

Two HTML INPUT elements of the type checkbox are rendered along with LABEL elements that contain text.

Raising and Handling Events and Determining Item Selection

As with all the other controls, you can raise an event when the CheckBox control changes from checked to unchecked, and vice versa. This event is the CheckedChanged event. You use OnCheckedChanged to indicate which method will handle this event. Note that, as with all the rest of the controls, the form will only post back on the change if AutoPostBack is set to true for the control unless a Button is used to submit the form.

Listing 8.17 Responding to Changes to the CheckBox Control
 [VisualBasic.NET] 01: <script runat="server" language="vb"> 02: 03:  public sub cb_CheckedChanged(sender as Object, e as EventArgs) 04: 05:   if(cb.Checked)then 06: 07:    cb.Text = "Item Is Checked" 08: 09:   else 10: 11:    cb.Text = "Item Is Not Checked" 12: 13:   end if 14: 15:  end sub 16: 17: </script> [C#.NET] 01: <script runat="server" language="C#"> 02: 03:  void cb_CheckedChanged(Object sender, EventArgs e) { 04: 05:   if(cb.Checked){ 06: 07:    cb.Text = "Item Is Checked"; 08: 09:   } else{ 10: 11:    cb.Text = "Item Is Not Checked"; 12: 13:   } 14: 15:  } 16: 17: </script> [VisualBasic.NET & C#.NET] 18 :<html> 19:  <body style="font-size:10"> 20:   <form runat="server"> 21: 22:    <asp:Checkbox runat="server" 23:     id="cb" 24:     Checked="true" 25:     Text="Item Is Checked" 26:     AutoPostBack="true" 27:     OnCheckedChanged="cb_CheckedChanged" 28:    /> 29: 30:   </form> 31:  </body> 32: </html> 

You'll notice similarities in the way all these controls work. The only differences are in the names of events and methods . The CheckBox control is located on lines 22 “28 and you'll notice that Checked is true , AutoPostBack is true , and OnCheckedChanged is cb_CheckedChanged ”which means cb_CheckedChanged will handle the CheckedChanged event.

Within cb_CheckedChanged (lines 3 “15) there is an if...then statement and depending on whether or not the cb CheckBox Checked attribute is true or false , the Text attribute of the CheckBox is changed. Figure 8.8 contains an illustration of the page on a post back after the CheckBox is unchecked.

Figure 8.8. The CheckBox is unchecked.

Binding a CheckBox to a Data Source

The CheckBox server control does not have a DataSource property, but you can bind individual attributes of the CheckBox to a data source as you have seen in the previous sections. For instance, you could set the Checked property to checked or unchecked, depending on whether or not a product is in stock.

only for RuBoard

Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170 © 2008-2017.
If you may any questions please contact us: