Asp tutorial. net checkboxlist value example
Checkboxlist is a set of check boxes. It makes no sense to assign values to checkboxlist.
Item assignment
Checkboxlist1.items. add (new listitem ("text 1", "value1 "));
Checkboxlist1.items. add (new listitem ("text 2", "value2 "));
Core code
String strapp = "current record, current page, all records ,";
String [] strtemp = strapp. split (',');
Foreach (string str in strtemp)
{
For (int I = 0; I <cbljl. items. count; I ++)
{
If (this. cbljl. items [I]. value = str)
{
This. cbljl. items [I]. selected = true;
}
}
}
Complete instance
<% @ Page language = vb debug = true %>
<% @ Import namespace = "system. data" %>
<Script runat = server>
Sub page_load (byval sender as object, byval e as eventargs)
If not ispostback then
Dim mydt as new datatable
Dim myrow as datarow
Mydt. columns. add (new datacolumn ("inclumentid ",_
Gettype (int32 )))
Mydt. columns. add (new datacolumn ("departmentname ",_
Gettype (string )))
Myrow = mydt. newrow ()
Myrow (0) = 1
Myrow (1) = "marketing"
Mydt. rows. add (myrow)
Myrow = mydt. newrow ()
Myrow (0) = 2
Myrow (1) = "sales"
Mydt. rows. add (myrow)
Myrow = mydt. newrow ()
Myrow (0) = 3
Myrow (1) = "support"
Mydt. rows. add (myrow)
Myrow = mydt. newrow ()
Myrow (0) = 4
Myrow (1) = "customer service"
Mydt. rows. add (myrow)
Cbl2.datasource = mydt
Cbl2.databind ()
End if
End sub
Sub cbladeclicked (sender as object, e as eventargs)
Dim I as integer
Lblmessage. text = "preferred office color (s): <br>"
For I = 0 to cbl1.items. count-1
If cbl1.items (I). selected then
Lblmessage. text = lblmessage. text _
& Cbl1.items (I). text & "<br>"
End if
Next
End sub
Sub cbl2_clicked (sender as object, e as eventargs)
Dim I as integer
Lblmessage2.text = "id of department (s) you work in: <br>"
For I = 0 to cbl2.items. count-1
If cbl2.items (I). selected then
Lblmessage2.text = lblmessage2.text _
& Cbl2.items (I). value & "<br>"
End if
Next
End sub
</Script>
<Html>
<Head>
<Title> checkboxlist control sample page </title>
</Head>
<Body>
<Form runat = "server">
<Font face = "tahoma">
<Asp: label
Id = "lblmessage"
Runat = "server"
Font-bold = "true"
Text = "preferred office color (s ):"
/>
<Asp: checkboxlist
Id = "cbl1"
Runat = "server"
Cellpadding = "5"
Cellspacing = "5"
Repeatcolumns = "3"
Repeatdirection = "vertical"
Repeatlayout = "table"
Textalign = "right"
Autopostback = "true"
Onselectedindexchanged = "cbl1_clicked"
>
<Asp: listitem> blue </asp: listitem>
<Asp: listitem> red </asp: listitem>
<Asp: listitem> green </asp: listitem>
<Asp: listitem> purple </asp: listitem>
<Asp: listitem> black </asp: listitem>
<Asp: listitem> gold </asp: listitem>
</Asp: checkboxlist>
<Hr>
<Asp: label
Id = "lblmessage2"
Runat = "server"
Font-bold = "true"
Text = "id of department (s) you work in: <br>"
/>
<Br>
<Asp: checkboxlist
Id = "cbl2"
Runat = "server"
Autopostback = "true"
Onselectedindexchanged = "cbl2_clicked"
Datatextfield = "departmentname"
Datavaluefield = "departmentid"
Backcolor = "lightyellow"
Forecolor = "darkred"
Bordercolor = "darkblue"
Borderstyle = 8
Textalign = "left"
Repeatlayout = "table"
>
</Asp: checkboxlist>
</Font>
</Form>
</Body>
</Html>