Monday, June 18, 2012

Set selected value of DropDownList in GridView EditItemTemplate


In Asp.Net the GridView supports Edit mode by means of configuring the controls in the EditItemTemplate.

For Textboxes, setting value in the Edit mode is straight forward, we can just set the Text property and the value will get displayed while editing the row.


<asp:TemplateField HeaderText="Description">
    <ItemTemplate>
      <asp:Label
ID="lblDesc"
runat="server"
Text='<%#Eval("DESC") %>'>
</asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
      <asp:TextBox
ID="txtLogDescChange"
Text='<%#Eval("DESC") %>'
runat="server"
            MaxLength="250">
</asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>

In the case of DropDownList, it is not straight forward, we shall see on how to accomplish this.
In the View mode, we shall display the value selected in the DropDownList in a Label, so that is straight forward.

In the Edit mode, we have to add a Label, in addition to the actual dropdown list, the value of the selected item should be set to this label. We will use this in the GridView RowDataBound event, to set the selected value for the DropDownList.

The code in the .aspx page is as follows.
<asp:TemplateField HeaderText="Status">
  <ItemTemplate>
<asp:Label
ID="lblStatus"
runat="server"
Text='<%#Eval("STATUS") %>'>
</asp:Label>
   </ItemTemplate>
   <EditItemTemplate>
      <asp:Label
ID=" lblStatusValue"
runat="server"
      Visible="false"
Text='<%#Eval("STATUS") %>'>
</asp:Label>
      <asp:DropDownList
ID="ddlStatus"
runat="server">
                <asp:ListItem Value="">Select</asp:ListItem>
                <asp:ListItem Value="New">New</asp:ListItem>
                <asp:ListItem Value="Open">Open</asp:ListItem>
                <asp:ListItem Value="InProgress">InProgress</asp:ListItem>
                <asp:ListItem Value="Closed">Closed</asp:ListItem>
      </asp:DropDownList>
    </EditItemTemplate>
  </asp:TemplateField>

Once this is done, navigate to the code-behind .aspx.cs file to the GridView’s RowDataBound event. Find the DropDownList control, get the value set is the label control and set as the selected value for the DropDownList control.

protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if ((e.Row.RowType == DataControlRowType.DataRow) && (grdData.EditIndex == e.Row.RowIndex))
   {
      DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus");
                ddlStatus.Items.FindByValue((e.Row.FindControl("lblStatusValue") as Label).Text).Selected = true;
    }      
}

Here the values in the DropDownList, ddlStatus are compared with the hidden label lblStatusValue which we used to store the selected value, when both the values match the corresponding item in the DropDownList is set as the selected item.

That’s it we have set the selected value for a DropDownList control, in the GridView in Edit mode.

Search Flipkart Products:
Flipkart.com

2 comments:

Buddha B'der said...

Excellent. Thank you. This approach works perfectly.

Buddha B'der said...
This comment has been removed by the author.