获取不到Repeater控件中的CheckBox选中状态

时间:2023-03-08 21:08:45

写在前面的话:在做一个项目的时候,需要使用到Repeater控件,并且在Repeater控件内放置了CheckBox控件来标志需要删除的行,选中后,在后台取到的CheckBox的值总是为false。最后发现是在PageLoad函数中没有判断是否是回发就绑定了Repeater控件的数据,那么每次进入页面CheckBox控件的值当然被刷新为false了。

前台页面:

 <div class="contianer p10">
<h3>
当前位置:<a href="Index.aspx" target="_self" >首页</a>>文章管理</h3>
<hr/>
<div class="content">
<asp:repeater ID="rptArticle" runat="server" ClientIDMode="Static" >
<HeaderTemplate>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<th width="6%">选择</th>
<th width="6%">编号</th>
<th align="left">文章标题</th>
<th width="16%">发布时间</th>
<th width="10%">操作</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="cb_id" CssClass="checkall" runat="server" /></td>
<td><asp:Label ID="lb_id" runat="server" Text='<%#Eval("aNo")%>'></asp:Label></td>
<td><a href="ArticleShow.aspx?id=<%#Eval("aNo") %>"><%#Eval("aTitle")%></a></td>
<td><%#string.Format("{0:g}", Eval("aDate"))%></td>
<td><span><a href="ArticleShow.aspx?id=<%#Eval("aNo") %>">修改</a></span></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
</div>
<div class="clear"></div>
<!-- 分页控件 -->
<div>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" HorizontalAlign="Right" ShowCustomInfoSection="Right"
Width="100%" Style="font-size: 12px" inputboxstyle="width:19px" CustomInfoHTML=""
ShowPageIndexBox="Always" AlwaysShow="false" FirstPageText="首页" LastPageText="尾页"
NextPageText="下一页" PrevPageText="上一页" CustomInfoStyle="FONT-SIZE: 12px" CurrentPageButtonClass="pagination"
PageSize="16" ForeColor="#1460AD" OnPageChanged="AspNetPager1_PageChanged">
</webdiyer:AspNetPager>
</div>
<!-- 分页控件end --> <br />
<asp:Button ID="BtnDelete" runat="server"
OnClientClick="return confirm( '确定要删除这些记录吗? ');"
onclick="BtnDelete_Click" Text="删除" />
&nbsp;
<asp:Label ID="lbMsg" runat="server" Text=""></asp:Label>
&nbsp;
<asp:Button ID="BtnAdd" runat="server" Text="添加"
onclick="BtnAdd_Click"/> </div>

后台页面:

 ArticleBLL bll = new ArticleBLL();
protected void Page_Load(object sender, EventArgs e)
{
this.lbMsg.Text = "";
if (!IsPostBack)
{
BindRepeater();
}
}
// 绑定数据
private void BindRepeater()
{
string strSql = "select * from article"; PagedDataSource ps = new PagedDataSource();
ps.DataSource = bll.GetDataList(strSql).DefaultView;
AspNetPager1.RecordCount = ps.Count;
ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - ;
ps.AllowPaging = true;
ps.PageSize = AspNetPager1.PageSize;
this.rptArticle.DataSource = ps;
this.rptArticle.DataBind();
}
// 分页控件事件绑定
public void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindRepeater();
}
// 添加文章
protected void BtnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("ArticleShow.aspx");
}
// 删除
protected void BtnDelete_Click(object sender, EventArgs e)
{
for (int i = ; i < rptArticle.Items.Count; i++)
{
int id = Convert.ToInt32(((Label)rptArticle.Items[i].FindControl("lb_id")).Text);
CheckBox cb = (CheckBox)rptArticle.Items[i].FindControl("cb_id");
if (cb.Checked)
{
bll.Delete(id.ToString());
}
}
this.lbMsg.Text = "删除成功";
}

处理办法是在PageLoad里面判断是否是回发就可以了。