Answered by:
uncheck the checkbox when deleting the gridview row

Question
-
User351619809 posted
Hello All,
I have a gridview and a checkbox outside the gridview. When I delete a row in the gridview, I want the checkbox to be unchecked if it is checked. Some how the checkbox does not get unchecked when I delete the gridview row. It works if the gridview is not enclosed in updatePanel otherwsie, it does not work.
below is the gridview and the code behind:<div> <asp:UpdatePanel runat="server" ID="UpdInfo" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" DataKeyNames="CartID" AllowPaging="false" PageSize="5" GridLines="None" OnRowCommand="grdShoppingCart_RowCommand" OnRowDataBound="grdShoppingCart_RowDataBound" OnRowDeleting="grdShoppingCart_RowDeleting"> <Columns> <asp:BoundField HeaderText="ID" DataField="CartID" /> <asp:BoundField HeaderText="Name" DataField="Name" /> <asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="150px" ControlStyle-CssClass="ss-row"> <ItemTemplate> <asp:HiddenField ID="hf_id" runat="server" Value='<%#Eval("CartID") %>' /> <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');" AlternateText="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </div> <asp:CheckBox ID="chkat" runat="server" Text="confirm" />
code behind:
private static DataTable _gridviewDT; public static DataTable GridviewDT { get { if (_gridviewDT is null) { _gridviewDT = new DataTable(); _gridviewDT.Columns.Add("CartID", typeof(int)); _gridviewDT.Columns.Add("Name", typeof(string)); _gridviewDT.Rows.Add(1, "test1"); _gridviewDT.Rows.Add(2, "test1"); _gridviewDT.Rows.Add(3, "test1"); _gridviewDT.Rows.Add(4, "test1"); _gridviewDT.Rows.Add(5, "test1"); _gridviewDT.Rows.Add(6, "test1"); } return _gridviewDT; } set { _gridviewDT = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView(); } } protected void BindGridView() { grdShoppingCart.DataSource = GridviewDT; grdShoppingCart.DataBind(); } protected void grdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e) { } protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e) { // this handler will be triggered as well var rowNum = e.RowIndex; chkat.Checked = false; }
when I do chkat.Checked = false; then the checkbox does not get unchecked. It stays checked. Bleow is the image:
I want the confirm check box to unchecked when the row is deleeted.
any help will be highly appreciated.
Tuesday, December 22, 2020 1:37 AM
Answers
-
User-939850651 posted
Hi anjaliagarwal5,
According to your description, I create a simple demo.
If I don’t understand what you mean, please refer to the following code:
<body> <form id="form1" runat="server"> <div> <asp:ScriptManager runat="server"></asp:ScriptManager> <asp:UpdatePanel runat="server" ID="UpdInfo" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" DataKeyNames="CartID" AllowPaging="false" PageSize="5" GridLines="None" OnRowCommand="grdShoppingCart_RowCommand" OnRowDataBound="grdShoppingCart_RowDataBound" OnRowDeleting="grdShoppingCart_RowDeleting"> <Columns> <asp:BoundField HeaderText="ID" DataField="CartID" /> <asp:BoundField HeaderText="Name" DataField="Name" /> <asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="150px" ControlStyle-CssClass="ss-row"> <ItemTemplate> <asp:HiddenField ID="hf_id" runat="server" Value='<%#Eval("CartID") %>' /> <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');" AlternateText="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="grdShoppingCart" /> </Triggers> </asp:UpdatePanel> </div> <asp:CheckBox ID="chkat" runat="server" Text="confirm" /> </form> </body>
protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e) { // this handler will be triggered as well var rowNum = e.RowIndex; chkat.Checked = false; DataRow row = _gridviewDT.Rows[rowNum]; _gridviewDT.Rows.Remove(row); grdShoppingCart.DataSource = _gridviewDT; grdShoppingCart.DataBind(); }
And I add some code in RowDeleting event for testing.
This is result:
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 22, 2020 6:17 AM
All replies
-
User-939850651 posted
Hi anjaliagarwal5,
According to your description, I create a simple demo.
If I don’t understand what you mean, please refer to the following code:
<body> <form id="form1" runat="server"> <div> <asp:ScriptManager runat="server"></asp:ScriptManager> <asp:UpdatePanel runat="server" ID="UpdInfo" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" DataKeyNames="CartID" AllowPaging="false" PageSize="5" GridLines="None" OnRowCommand="grdShoppingCart_RowCommand" OnRowDataBound="grdShoppingCart_RowDataBound" OnRowDeleting="grdShoppingCart_RowDeleting"> <Columns> <asp:BoundField HeaderText="ID" DataField="CartID" /> <asp:BoundField HeaderText="Name" DataField="Name" /> <asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="150px" ControlStyle-CssClass="ss-row"> <ItemTemplate> <asp:HiddenField ID="hf_id" runat="server" Value='<%#Eval("CartID") %>' /> <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');" AlternateText="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="grdShoppingCart" /> </Triggers> </asp:UpdatePanel> </div> <asp:CheckBox ID="chkat" runat="server" Text="confirm" /> </form> </body>
protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e) { // this handler will be triggered as well var rowNum = e.RowIndex; chkat.Checked = false; DataRow row = _gridviewDT.Rows[rowNum]; _gridviewDT.Rows.Remove(row); grdShoppingCart.DataSource = _gridviewDT; grdShoppingCart.DataBind(); }
And I add some code in RowDeleting event for testing.
This is result:
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 22, 2020 6:17 AM -
User351619809 posted
Thank You!
Tuesday, December 22, 2020 4:42 PM