locked
Find checkbox column in gridview OnCheckedChange event RRS feed

  • Question

  • User2091173246 posted

    I have few gridview columns with checkboxes control that has an autopostback='true' to update a db.

    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 2]"))%>' OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />
    </ItemTemplate>

    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 1]"))%>' OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />
    </ItemTemplate

    and a few more....

    How can i just focus on column "This week -2" and look if that has true of false value checked?

    protected void myCheckBox_OnCheckedChange(object sender, EventArgs e)
    {

    CheckBox myCheckBox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)myCheckBox.NamingContainer;
    bool status = myCheckBox.Checked;


    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;


    using (SqlConnection con = new SqlConnection(CS))
    {
    SqlCommand cmd = new SqlCommand("sp_tblPlanningChecked", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@PlanningID", row.Cells[1].Text);
    cmd.Parameters.AddWithValue("@ThisWeekMinus2", myCheckBox.Checked);
    cmd.Parameters.AddWithValue("@ThisWeekMinus1", myCheckBox.Checked);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    }

    }

    This code "works" however it updated both columns to true because myCheckbox.checked is "always true"  if one of the column rows are checked.

    Can tweek my code fo only focus on "This week -2" column?

    Monday, April 16, 2018 12:47 PM

All replies

  • User283571144 posted

    Hi Skwizzy,

    According to your codes, I have created a test demo on my side, it works well.

    I could get the right check box chekced value in the oncheckedchanged method.

    More details, you could refer to below codes and result image:

    ASPX:

        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server">
                    <Columns>
                        <asp:TemplateField  HeaderText="This Week - 2" ShowHeader="true">
                            <ItemTemplate >
                                <asp:CheckBox ID="CheckBox2" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 2]"))%>' OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" Style="text-align: center" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField  HeaderText="This Week - 1" ShowHeader="true">
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox3" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 1]"))%>' OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" Style="text-align: center" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>

    Code-behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
    
                    LoadData();
                }
                
            }
            public void LoadData()
            {
                DataTable d1 = new DataTable() { Columns = { "ID", "Product_Name", "Type" , "This Week - 2", "This Week - 1" } };
                d1.Rows.Add("1", "aa", "bb", true, true);
                d1.Rows.Add("2", "bb", "cc", true,true);
                d1.Rows.Add("3", "cc", "dd", true, true);
                GridView1.DataSource = d1;
                GridView1.DataBind();
            }
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                CheckBox myCheckBox = (CheckBox)sender;
                bool status = myCheckBox.Checked;
                Response.Write(status);
            }

    Result:

    Best Regards,

    Brando

    Tuesday, April 17, 2018 6:40 AM
  • User2091173246 posted

    Thank you i will look at this.

    Tuesday, April 17, 2018 7:36 AM