checkBox遍历GridView 现在GridView不能分页
应该是IsPostBack的问题,如果有IsPostBack,checkBox遍历不好用,提不到数据,如果不用的话,分页又不好用,请问有什么好方法?
- HTML code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnRowCreated="GridView1_RowCreated" CellPadding="4" ForeColor="#333333" CssClass="mytable" GridLines="None" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging" > <Columns> <asp:TemplateField HeaderText="序号"> <ItemTemplate> <asp:CheckBox ID="CheckBox2" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="店铺名称" SortExpression="ShopName"> <ItemStyle Wrap="False" Width="150px" /> <ItemTemplate> <asp:Label ID="Label1" Width="150px" runat="server" Text='<%# Bind("ShopName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <PagerTemplate> 当前第: <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> 页/共: <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton> <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton> <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton> <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton> </PagerTemplate> </asp:GridView>- C# code
if (!IsPostBack) { dbcom_srcView = db.GetSqlStringCommand("select taoCouponsID,taoCouponsShopName,taoCouponsShopContent from TS_TAOCOUPONS"); db.ExecuteNonQuery(dbcom_srcView); if (db.ExecuteDataSet(dbcom_srcView).Tables[0].Rows.Count > 0) { GridView1.DataSource = db.ExecuteDataSet(dbcom_srcView).Tables[0]; GridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < this.GridView1.Rows.Count; i++) { CheckBox chksel = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox2"); if (chksel.Checked) { delid += ((Label)(this.GridView1.Rows[i].FindControl("Label1"))).Text; } } Label3.Text = delid; }
[解决办法]
分页执行的IsPostBack了,没有代码可以执行了。你应该去掉判断
或者添加分页事件绑定数据,如GridView1_PageIndexChanging等事件