前台代码:
<asp:Repeater ID="rptList" runat="server" onitemdatabound="doSomething">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ltable">
<tr>
<th width="8%">选择</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:CheckBox ID="chkId" CssClass="checkall" runat="server" style="vertical-align:middle;" />
<asp:HiddenField ID="hidId" Value='<%#Eval("id")%>' runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台代码:
1.绑定数据:
DataSet source = bll.GetList(.......);
rptList.DataSource = source;
rptList.DataBind();
2.操作 单选框
var selectCount=0;//选中了几个
var count=0;//选中值
for (int i = 0; i < rptList.Items.Count; i++)
{
int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);//隐藏域中的值
CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");//单选
if (cb.Checked)
{
selectCount++;
count+=id;
}
}
3.onitemdatabound 绑定数据后的方法
protected void doSomething(object sender, RepeaterItemEventArgs e)//Repeater控件 e
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HiddenField hidLayer = (HiddenField)e.Item.FindControl("hidId");//获取控件
int classLayer = Convert.ToInt32(hidLayer.Value);
}}