ASP.Net GridView: Update Database Field using DropDownList and Display it in Label in ItemTemplate

on Feb 23, 2013 04:03 AM

i have three saved values in database 


i want to showing status in label for 1 , Pending


 <asp:Label ID="lblLeaveStatus" Visible = "true" runat="server" Text='<%# Eval("Rec_Status").ToString() == "1" ? "Pending": Eval("Rec_Status") %>'>></asp:Label>

how can i show it with all 1,2,3??

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Feb 23, 2013 04:47 AM

First of all you will have to make changes in design like this

 <asp:GridView ID="gvEmployeeLeaves" runat="server" CellPadding="0" CellSpacing="0"
        AutoGenerateColumns="false" Width="300px">
            <asp:BoundField DataField="EmpId" HeaderStyle-Width="100px" HeaderText="Employee ID" />
            <asp:BoundField DataField="Name" HeaderStyle-Width="100px" HeaderText="Name" />
            <asp:TemplateField HeaderText="Leave Status" HeaderStyle-Width="100px">
                    <asp:DropDownList ID="ddlLeaveStatus" runat="server" OnSelectedIndexChanged="OnLeaveStatusChanged" AutoPostBack="true">
                        <asp:ListItem Text="Pending" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Solved" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Escalate" Value="3"></asp:ListItem>
            <asp:TemplateField HeaderText="Leave Status" HeaderStyle-Width="100px">
                    <asp:Label ID="lblLeaveStatus" runat="server"></asp:Label>
            <asp:TemplateField HeaderText="Update" HeaderStyle-Width="100px">
                    <asp:Button ID="btnUpdate" runat="server" OnClick="OnUpdateClick" Text="Update" CommandArgument='<%#Eval("EmpId") %>' />

You will have to set your dropdownlist AutoPostBack to true and you will have to add the OnSelectedIndexChanged of the drop down list and you will have to add the event of dropdownlist in your code and write the following code in this event

   protected void OnLeaveStatusChanged(object sender, EventArgs e)
            GridViewRow row = (sender as DropDownList).NamingContainer as GridViewRow;
            DropDownList ddlLeaveStatus = row.FindControl("ddlLeaveStatus") as DropDownList;
            Label lblLeaveStatus = row.FindControl("lblLeaveStatus") as Label;
            lblLeaveStatus.Text = ddlLeaveStatus.SelectedItem.Text;