如何在C#中的转发器中选中CheckBox时避免页面刷新?

时间:2014-01-22 07:32:31

标签: c# asp.net

在带有转发器控件的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>
  

代码背后

      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 checkstate)    {

   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;
       }
   }

6 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

在复选框上将AutoPostBack属性设置为false。

答案 2 :(得分:1)

我相信,问题的症结在于:

“当我从下拉列表中选择任何值时,我会从数据库中加载一些依赖于此选定值的数据,每当选择更改页面刷新时我都会遇到问题。”

有很多方法可以实现这一目标,但可能需要进行一些重组才能产生预期的效果。一个相对简单的方法是:

 <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
            <ContentTemplate>
               <asp:CheckBox ID="chk_select" AutoPostBack="true" runat="server" OnCheckedChanged="chk_select_CheckedChanged"/>                  
           </ContentTemplate>
           <Triggers>
                <asp:Asyncpostbacktrigger controlid="chk_select" eventname="SelectedIndexChanged" />
           </Triggers>
 </asp:UpdatePanel>

答案 3 :(得分:0)

设置Autopostback checkbox属性的值postback导致false到{{1}}

答案 4 :(得分:0)

您可以将CheckBox的AutoPostBack属性设置为false,否则您也可以使用Ajax UpdatePanel控件并将CheckBox放入其中,以便不会重新加载整个页面,只刷新复选框值。

答案 5 :(得分:0)

之前的答案几乎是正确的,除了ASP.NET复选框没有SelectedIndexChanged事件。它应该是CheckedChanged

var RS = document.getElementById("right");

while( LS.lastChild )
{
    RS.appendChild( LS.lastChild.cloneNode(true) );
    LS.removeChild( LS.lastChild );
}