jeudi 3 novembre 2022

On gridview Row Editing I want to set checkbox unchecked always

I have a gridview where I want to display some data. And in that gridview, I have a checkbox and Edit button.

By default all checkbox are checked and once any row checkbox is unchecked the Edit button becomes enabled.

So here the issue is whenever the Edit button is clicked to update something the checkbox again gets checked. I dont want it that way. Below is my whole code for the same. PLease help how to handle it.

<asp:GridView ID="rptHotoIPDataInfo" runat="server" AutoGenerateColumns="False" CssClass="table table-bordered"
                AllowPaging="True" PageSize="20" OnPageIndexChanging="rptHotoIPDataInfo_PageIndexChanging"
                DataKeyNames="SAP_ID" OnRowEditing="rptHotoIPDataInfo_RowEditing" OnRowUpdating="rptHotoIPDataInfo_RowUpdating"
                OnRowCancelingEdit="rptHotoIPDataInfo_RowCancelingEdit" OnRowDataBound="rptHotoIPDataInfo_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                            <asp:CheckBox ID="chkRow" runat="server" Checked="true" Width="10px" OnCheckedChanged="chkRow_CheckedChanged"
                                AutoPostBack="true" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" Enabled="false" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update" />
                            <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Rejected Remarks">
                        <ItemTemplate>
                            <asp:Label ID="lblRemarks" runat="server" Text='<%#Eval("REJECT_REMARKS") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtRemarks" runat="server" Text='<%#Eval("REJECT_REMARKS") %>' Width="150px"></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:BoundField DataField="SAP_ID" HeaderText="SAP ID">
                        <ItemStyle CssClass="labelbold" Width="80px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="CR_NO" HeaderText="Description" Visible="False">
                        <ItemStyle CssClass="labelbold" Width="100px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="R4GSTATE_CODE" HeaderText="State Code">
                        <ItemStyle CssClass="labelbold" Width="30px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="HOTO_STATUS" HeaderText="Hoto Status">
                        <ItemStyle CssClass="labelbold" Width="50px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="CR_Justifications" HeaderText="CR Justifications">
                        <ItemStyle CssClass="labelbold" Width="100px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="APPROVE_REJECT" HeaderText="Remarks">
                        <ItemStyle CssClass="labelbold" Width="10px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="REJECTED_GRP" HeaderText="Remarks">
                        <ItemStyle CssClass="labelbold" Width="10px" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>

Also the code behind code:-

protected void rptHotoIPDataInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
    rptHotoIPDataInfo.EditIndex = e.NewEditIndex;
    BindHotoIPNonIPSubmit();
}
protected void rptHotoIPDataInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Label id = rptHotoIPDataInfo.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
    TextBox txt_Remarks = rptHotoIPDataInfo.Rows[e.RowIndex].FindControl("txtRemarks") as TextBox;

    string strID = id.Text;
    string strRemarks = txt_Remarks.Text;
    string rejected_by = CurrentUserName;
    string rejected_group_id = ddlApprovalStatus.SelectedValue;

    var splitValue = rejected_group_id.Split('-');
    var middleValue = splitValue[1];

    bool updateData = false;

    if (id != null)
    {

        updateData = UpdateRejectInfo(strID, strRemarks, rejected_by, middleValue);
        if (updateData)
        {
            rptHotoIPDataInfo.EditIndex = -1;
            BindHotoIPNonIPSubmit();
        }
    }
}
protected void rptHotoIPDataInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    rptHotoIPDataInfo.EditIndex = -1;
    BindHotoIPNonIPSubmit();
}    
protected void rptHotoIPDataInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[9].Text == "R")
        {
            // e.Row.BackColor = System.Drawing.Color.Gray;                
        }
    }
}
protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
    for (int i = 0; i < rptHotoIPDataInfo.Rows.Count; i++)
    {
        CheckBox chk = (CheckBox)rptHotoIPDataInfo.Rows[i].Cells[0].FindControl("chkRow");
        Button btnEdit = (Button)rptHotoIPDataInfo.Rows[i].Cells[0].FindControl("btn_Edit");
        if (chk.Checked)
        {
            btnEdit.Enabled = false;
        }
        else
        {
            btnEdit.Enabled = true;
        }
    }          
}



Aucun commentaire:

Enregistrer un commentaire