0

I have found this question a few places but no solutions...

I have a checkbox in a gridview:

<asp:TemplateField HeaderText="Closed?">
    <ItemTemplate >
        <asp:CheckBox ID="Status_CB"  runat="server" AutoPostBack="true" 
            OnCheckedChanged="Status_CB_CheckedChanged" 
            EnableViewState="true" ViewStateMode="Enabled" 
            Checked='<%# Convert.ToString(Eval("cStatus")) == "1" ? true : false %>'/>
    </ItemTemplate>
</asp:TemplateField>

codebehind:

protected void Page_Load(object sender, EventArgs e) {
    if (!int.TryParse(Session["FacilityID"].ToString(), out FId)) {
        FId = 0;
    }
    if (!Page.IsPostBack) {
        if (!string.IsNullOrEmpty(Request.QueryString.Get("WorkCenter"))) {
            wc = Request.QueryString.Get("WorkCenter");
            WorkcenterHeader.InnerText = wc + " Schedule ";
            HiddenWorkCenter.Value = c;
        }
        if (!SQLHasData()) {
            SavePrioritiesToSQL();
        }
        BindGrid();
    }
}
protected void Status_CB_CheckedChanged(object sender, EventArgs e) {
    CheckBox cb = (CheckBox)sender;
    GridViewRow row = (GridViewRow) cb.Parent.Parent;
}

When i check the box originally, it works. When i uncheck it, the breakpoint i have on the first line of Status_CB_CheckedChanged does not fire at all.

What am i missing any one know?

UPDATE - here is the table, it is nested. i wonder if that is the reason it will not call the postback on uncheck...

UPDATE - ok i gave up, this must be a bug with nested gridview in asp so if you have a nested gridview, i recommend not using checkboxes. I switched mine to a text field of the cStatus "open" or "closed" and use a button with a command argument that is the row index:

<asp:GridView ID="JobInfo_GV" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid2" OnRowCommand="JobInfo_GV_RowCommand">
<asp:BoundField DataField="cStatus" HeaderText="Status" ReadOnly="True" HeaderStyle-CssClass="center-row" ItemStyle-CssClass="center-row"/>  
<asp:TemplateField HeaderText="Update">
    <ItemTemplate >
      <asp:Button id="UpdateClosed" commandname="Select" buttontype="button" Text="ToggleStatus"  runat="server" CommandArgument='<%# Container.DataItemIndex %>'/>
    </ItemTemplate>
</asp:TemplateField>

then the C#:

protected void JobInfo_GV_RowCommand(object sender, GridViewCommandEventArgs e) {
    var grid = (GridView)sender;
    var errorMessage = string.Empty;
    if (grid != null) {
        int index = 0;
        if (int.TryParse(e.CommandArgument.ToString(), out index) ){
        GridViewRow row = grid.Rows[index];
6
  • I tested your code. It works just fine! Can you please post your complete aspx and c# code? Commented Mar 19, 2020 at 19:05
  • I am wondering if it's because the gridview is a templateview in a gridview then... Commented Mar 19, 2020 at 19:15
  • Please check my answer and compare it to your code. My answer works as you expect. Commented Mar 19, 2020 at 19:21
  • Updated to add my full table. Your solution works, until i wrap it in the table and then it fails Commented Mar 19, 2020 at 19:42
  • You need to set AutoPostBack="true" for the Checkbox control. your original code had that but your new code does not have it! Is'nt that the issue? Commented Mar 19, 2020 at 19:53

1 Answer 1

1

I just created a project and reused your code as part of it. It works as you expect:

Note you need to set AutoPostBack="true" for the Checkbox control

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GV" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name">
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Closed?">
                        <ItemTemplate>
                            <asp:CheckBox ID="Status_CB" runat="server" AutoPostBack="true"
                                OnCheckedChanged="Status_CB_CheckedChanged"
                                EnableViewState="true" ViewStateMode="Enabled"
                                Checked='<%# Convert.ToString(Eval("cStatus")) == "1" ? true : false %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

And the Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGrid();
        }

        public class DataS
        {
            public int cStatus { get; set; }
            public string Name { get; set; }
        }
        private void BindGrid()
        {
            List<DataS> list = new List<DataS>() { new DataS() { Name = "Name1", cStatus = 1 }, new DataS() { Name = "Name2", cStatus = 1 }, new DataS() { Name = "Name3", cStatus = 0 } };
            GV.DataSource = list;
            GV.DataBind();
        }

        protected void Status_CB_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            GridViewRow row = (GridViewRow)cb.Parent.Parent;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

the autopostback was set to true. I removed it when i started changing the solution to use textboxes and a button instead of a the bugged checkbox. There is a workaround in the origional answer now if any one is struggling with a checkbox in a nested gridview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.