asp教程.net checkboxlist 取值实例
checkboxlist是复选框的集合,对checkboxlist赋值是没有意义的,要对里面的选
项赋值
checkboxlist1.items.add(new listitem("文本1", "value1"));
checkboxlist1.items.add(new listitem("文本2", "value2"));
核心代码
string strapp = "当前记录,当前页,全部记录,";
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;
}
}
}
完整实例
<%@ 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("departmentid", _
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 cbl1_clicked(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>