dimanche 27 septembre 2020

Checking when there is already approved checkbox in the gridview in C#

I have a checkbox in a gridview row.

I know how to disable it using RowDataBound and using a database value as a condition to disable or not.

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem != null)
        {
            int status = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "tstatus"));
            CheckBox chkRow = (CheckBox)e.Row.FindControl("chkRow");

            if (status > 0)
            {
                chkRow.Enabled = false;
                chkRow.Checked = true;
                chkRow.ToolTip = "Approved";
            }
        }
    }
}

my problem is checking when there is already approved checkbox in the gridview

foreach (GridViewRow row in gvProducts.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (CheckBox)(row.Cells[0].FindControl("chkRow"));

        if (chkRow.Checked && chkRow.ToolTip == "to be approved")
        {
            int oIndividualID = Convert.ToInt32((gvProducts.DataKeys[row.RowIndex].Value));

            using (MySqlConnection myConnectionString =
              new MySqlConnection(ConfigurationManager.ConnectionStrings["cn2"].ConnectionString))
            {
                using (MySqlCommand cmd =
                    new MySqlCommand(sql, myConnectionString))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection.Open();
                    cmd.CommandText = "SP";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("tid", oIndividualID.ToString());
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                    Bindgrid();
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Approved');", true);
                }
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Not checkbox selected');", true);
        }
    }
}

in this case, i.e. when there is already a grid row with checkbox selected, if I try to select a new checkbox the return on the browser is

'Not checkbox selected'

even if the database table updates correctly

this is the page markup

<asp:TemplateField HeaderText="Select">
    <ItemTemplate>
        <asp:CheckBox ID="chkRow" runat="server"
            ToolTip="to be approved" />
    </ItemTemplate>
</asp:TemplateField>

Can anyone help please?




Aucun commentaire:

Enregistrer un commentaire