Skip to main content
 首页 » 编程设计

c#之在 C# 的中继器中选择 CheckBox 时如何避免页面刷新

2024年02月13日12emanlee

在带有转发器控件的 asp.net 网页中,当我选中标题中的复选框以选中项目模板行中的所有复选框时,如何避免整个页面刷新?

我的项目基于 asp.net C#,以 SQL Server 作为数据库。

        <asp:Repeater ID="Repeater_product_detail" runat="server" OnItemCommand="Repeater_product_detail_ItemCommand" OnItemDataBound="Repeater_product_detail_ItemDataBound"> 
    <HeaderTemplate> 
        <table  class="table table-striped table-bordered "> 
            <thead> 
                <tr> 
                              <td> <asp:CheckBox ID="chk_select" AutoPostBack="true" runat="server" OnCheckedChanged="chk_select_CheckedChanged"/> </td> 
                              <th>SubCategory</th> 
                              <th>Product Name</th> 
                              <th>Product image</th> 
                              <th>Product Price</th> 
                              <th>in stock</th> 
                              <th>Type for</th> 
                              <th>Action</th> 
                   </tr>                   
            </thead> 
            <tbody> 
 
    </HeaderTemplate> 
    <ItemTemplate> 
        <tr> 
            <td>   
                 <asp:CheckBox ID="chkDelete" AutoPostBack="true"  runat="server" />  
                 <asp:Label ID="lbl_id" Visible="false" runat="server" Text='<%# ("int_product_id") %>'></asp:Label> 
            </td> 
             <td>  
                 <asp:Label ID="lbl_sub_cate" runat="server" Text='<%# Eval("txt_sub_category_name") %>'></asp:Label> 
                 <asp:DropDownList ID="ddl_sub_category" Width="100px" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "txt_sub_category_name") %>' runat="server" Visible="false" > </asp:DropDownList> 
 
             </td> 
 
            <td> <asp:Label ID="lbl_product_name" runat="server" Text='<%# Eval("txt_product_name") %>'></asp:Label> 
            <asp:TextBox ID="txt_product_name" BackColor="#d4d0c8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_product_name")%>' Visible="false"></asp:TextBox> 
            </td> 
 
            <td> <%--<asp:Label ID="lbl_product_image" runat="server" Text='<%# Eval("product_img_small") %>'></asp:Label>--%> 
                  <asp:Image ID="Image1" Height="50px" Width="50px" ImageUrl='<%# Eval("product_img_small") %>' runat="server" /> 
            </td> 
 
 
            <td> <asp:Label ID="lbl_product_price" runat="server" Text='<%# Eval("txt_product_price") %>'></asp:Label> 
             <asp:TextBox ID="txt_product_price" Width="60px" BackColor="#d4d0c8" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txt_product_price")%>' Visible="false"></asp:TextBox> 
            </td> 
            <td> <asp:Label ID="lbl_stock" runat="server" Text='<%# Eval("in_stock") %>'></asp:Label> 
             <asp:TextBox ID="txt_stock" BackColor="#d4d0c8" Width="60px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "in_stock")%>' Visible="false"></asp:TextBox> 
            </td> 
            <td> <asp:Label ID="lbl_type" runat="server" Text='<%# Eval("cate_type") %>'></asp:Label> 
            <asp:DropDownList ID="ddl_type" runat="server" Width="60px" DataTextField="cate_type" Visible="false"></asp:DropDownList> 
            </td> 
            <td> 
                <asp:LinkButton ID="lnk_edit" CommandArgument='<%# Eval("int_product_id") %>' CommandName="edit" runat="server">Edit</asp:LinkButton> 
                 <asp:LinkButton ID="lnk_update" CommandArgument='<%# Eval("int_product_id") %>' Visible="false" CommandName="update" runat="server">Update</asp:LinkButton> 
                 <asp:LinkButton ID="lnk_cancel" CommandArgument='<%# Eval("int_product_id") %>' Visible="false" CommandName="cancel" runat="server">Cancel</asp:LinkButton> 
                 <asp:LinkButton ID="lnk_delete" CommandArgument='<%# Eval("int_product_id") %>' CommandName="delete" OnClientClick='javascript:return confirm("Are you sure you want to delete?")'  runat="server">Delete</asp:LinkButton> 
 
            </td> 
 
 
        </tr> 
    </ItemTemplate> 
 
    <FooterTemplate> 
             <tr style="background-color:#15880a"> 
             <td colspan="8">  
        </tbody> 
      </table> 
    </FooterTemplate> 
 
</asp:Repeater> 
<asp:LinkButton ID="lnk_del_selected" CommandArgument='<%# Eval("int_product_id") %>'  OnClientClick='javascript:return confirm("Are you sure you want to delete?")' runat="server" OnClick="lnk_del_selected_Click">Deleted Selected</asp:LinkButton> 

Behind Code

      protected void chk_select_CheckedChanged(object sender, EventArgs e) 
{ 
    Control header_control = Repeater_product_detail.Controls[0].Controls[0];   // Find header Template's Items 
 
    CheckBox chk = header_control.FindControl("chk_select") as CheckBox; 
    if (!chk.Checked) 
            { 
                toggleCheckState(false); 
            } 
            else 
            { 
                toggleCheckState(true); 
            } 
} 

public void toggleCheckState( bool 检查状态) {

   foreach (RepeaterItem item in Repeater_product_detail.Items)   // Find Item Template's Items 
   { 
       if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item) 
       { 
           CheckBox chk_delete = (CheckBox)item.FindControl("chkDelete"); 
           chk_delete.Checked = checkstate; 
       } 
   } 

请您参考如下方法:

在复选框属性中设置 AutoPostBack="false"。