samedi 21 octobre 2017

Checking and Unchecking single checkbox and saving value in database in grid view in edit button click even

I need a Status column in database which contains 1,0 for representing Employee's Status "Active" and "Inactive" respectively. I want edit this record through front-end,having grid-view, and want to perform on GridView's update event, after clicking on Edit Button. I have a TemplateField having header-text "Status". I am unable to update changed value of checkbox.

Moreover, if will get checked or unchecked CheckBox on the basis of value stored in database, if it is 0 then CheckBox will be uncheked, otherwise will be checked. If user click on edit button, and then check or uncheck any Check-box, then on the basis of this, value should be updated in database.

**DataBase:-**

    CREATE TABLE [dbo].[Employee](
        [Employee_ID] [int] IDENTITY(1,1) NOT NULL,
        [Employee_Name] [varchar](50) NULL,
        [Employee_Address] [varchar](100) NULL,
        [Emp_Status] [int] NULL,
    PRIMARY KEY CLUSTERED 
    (
        [Employee_ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON

**Front-End:-**

**Mark-up:**



    <asp:GridView ID="GV_Product" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="GV_Product_RowEditing" OnRowUpdating="GV_Product_RowUpdating" OnRowDeleting="GV_Product_RowDeleting" OnRowCancelingEdit="GV_Product_RowCancelingEdit" OnRowCommand="GV_Product_RowCommand" AllowPaging="true" PageSize="5" OnPageIndexChanging="GV_Product_PageIndexChanging">
                        <AlternatingRowStyle BackColor="Gainsboro" />

                        <Columns>
                            <asp:TemplateField HeaderText="Employee ID">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_ID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_ID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Employee Name">
                              <ItemTemplate>
                                  <asp:TextBox ID="txt_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
                              </ItemTemplate>

                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEdit_Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Name") %>'></asp:TextBox>
                                </EditItemTemplate>

                                <FooterTemplate>
                                    <asp:TextBox ID="txtAdd_Name" runat="server"></asp:TextBox>
                                    <%--<asp:RequiredFieldValidator ID="txtName" runat="server" ControlToValidate="txtAdd_Name" ErrorMessage="Please enter Employee Name"></asp:RequiredFieldValidator>--%>
                                </FooterTemplate>

                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Employee Address">
                                <ItemTemplate>
                                    <asp:TextBox ID="txt_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
                                </ItemTemplate>

                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEdit_Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Employee_Address") %>'></asp:TextBox>
                                </EditItemTemplate>

                                <FooterTemplate>
                                    <asp:TextBox ID="txtAdd_Address" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                                    <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />

                                </ItemTemplate> 

                                <EditItemTemplate>
                                    <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
                                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                                </EditItemTemplate>

                                <FooterTemplate>
                                    <asp:LinkButton ID="lblAdd" runat="server" Width="100px" Text="Add" CommandName="ADD"></asp:LinkButton>
                                </FooterTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Status">
                                <ItemTemplate>
                                    <asp:CheckBox ID="ChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>' />
                                </ItemTemplate>

                                <EditItemTemplate>
                                    <asp:CheckBox ID="EditChkBox" runat="server" Checked='<%# GetStatus(DataBinder.Eval(Container.DataItem,"Emp_Status").ToString() )%>'/>
                                    <%--<asp:CheckBoxList ID="ChkBoxList" runat="server">
                                        <asp:ListItem>1</asp:ListItem>
                                    </asp:CheckBoxList>--%>
                                </EditItemTemplate>
                            </asp:TemplateField>


                        </Columns>


 <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#0000A9" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#000065" />
                </asp:GridView>

**Code:**

    protected bool GetStatus(string str)
        {
            if (str=="1")
            {
                return true;
            }

            else
            {
                return false;
            }
        }




Aucun commentaire:

Enregistrer un commentaire