写在前面的话:在做一个项目的时候,需要使用到Repeater控件,并且在Repeater控件内放置了CheckBox控件来标志需要删除的行,选中后,在后台取到的CheckBox的值总是为false。最后发现是在PageLoad函数中没有判断是否是回发就绑定了Repeater控件的数据,那么每次进入页面CheckBox控件的值当然被刷新为false了。
前台页面:
1 <div class="contianer p10">
2 <h3>
3 当前位置:<a href="Index.aspx" target="_self" >首页</a>>文章管理</h3>
4 <hr/>
5 <div class="content">
6 <asp:repeater ID="rptArticle" runat="server" ClientIDMode="Static" >
7 <HeaderTemplate>
8 <table border="0" cellspacing="0" cellpadding="0" width="100%">
9 <tr>
10 <th width="6%">选择</th>
11 <th width="6%">编号</th>
12 <th align="left">文章标题</th>
13 <th width="16%">发布时间</th>
14 <th width="10%">操作</th>
15 </tr>
16 </HeaderTemplate>
17 <ItemTemplate>
18 <tr>
19 <td><asp:CheckBox ID="cb_id" CssClass="checkall" runat="server" /></td>
20 <td><asp:Label ID="lb_id" runat="server" Text=‘<%#Eval("aNo")%>‘></asp:Label></td>
21 <td><a href="ArticleShow.aspx?id=<%#Eval("aNo") %>"><%#Eval("aTitle")%></a></td>
22 <td><%#string.Format("{0:g}", Eval("aDate"))%></td>
23 <td><span><a href="ArticleShow.aspx?id=<%#Eval("aNo") %>">修改</a></span></td>
24 </tr>
25 </ItemTemplate>
26 <FooterTemplate>
27 </table>
28 </FooterTemplate>
29 </asp:repeater>
30 </div>
31 <div class="clear"></div>
32 <!-- 分页控件 -->
33 <div>
34 <webdiyer:AspNetPager ID="AspNetPager1" runat="server" HorizontalAlign="Right" ShowCustomInfoSection="Right"
35 Width="100%" Style="font-size: 12px" inputboxstyle="width:19px" CustomInfoHTML=""
36 ShowPageIndexBox="Always" AlwaysShow="false" FirstPageText="首页" LastPageText="尾页"
37 NextPageText="下一页" PrevPageText="上一页" CustomInfoStyle="FONT-SIZE: 12px" CurrentPageButtonClass="pagination"
38 PageSize="16" ForeColor="#1460AD" OnPageChanged="AspNetPager1_PageChanged">
39 </webdiyer:AspNetPager>
40 </div>
41 <!-- 分页控件end -->
42
43 <br />
44 <asp:Button ID="BtnDelete" runat="server"
45 OnClientClick="return confirm( ‘确定要删除这些记录吗? ‘);"
46 onclick="BtnDelete_Click" Text="删除" />
47
48 <asp:Label ID="lbMsg" runat="server" Text=""></asp:Label>
49
50 <asp:Button ID="BtnAdd" runat="server" Text="添加"
51 onclick="BtnAdd_Click"/>
52
53 </div>
后台页面:
1 ArticleBLL bll = new ArticleBLL();
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 this.lbMsg.Text = "";
5 if (!IsPostBack)
6 {
7 BindRepeater();
8 }
9 }
10 // 绑定数据
11 private void BindRepeater()
12 {
13 string strSql = "select * from article";
14
15 PagedDataSource ps = new PagedDataSource();
16 ps.DataSource = bll.GetDataList(strSql).DefaultView;
17 AspNetPager1.RecordCount = ps.Count;
18 ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
19 ps.AllowPaging = true;
20 ps.PageSize = AspNetPager1.PageSize;
21 this.rptArticle.DataSource = ps;
22 this.rptArticle.DataBind();
23 }
24 // 分页控件事件绑定
25 public void AspNetPager1_PageChanged(object sender, EventArgs e)
26 {
27 BindRepeater();
28 }
29 // 添加文章
30 protected void BtnAdd_Click(object sender, EventArgs e)
31 {
32 Response.Redirect("ArticleShow.aspx");
33 }
34 // 删除
35 protected void BtnDelete_Click(object sender, EventArgs e)
36 {
37 for (int i = 0; i < rptArticle.Items.Count; i++)
38 {
39 int id = Convert.ToInt32(((Label)rptArticle.Items[i].FindControl("lb_id")).Text);
40 CheckBox cb = (CheckBox)rptArticle.Items[i].FindControl("cb_id");
41 if (cb.Checked)
42 {
43 bll.Delete(id.ToString());
44 }
45 }
46 this.lbMsg.Text = "删除成功";
47 }
处理办法是在PageLoad里面判断是否是回发就可以了。
时间: 2024-10-05 03:57:09