Search code examples
c#asp.netgridviewitemtemplatedropdownbox

Gridview Itemtemplate DropDownList Enabled


I'd like the DropDownList to be disabled and enable it only after I click on the edit link on Gridview. As of now, it is showing the DropDownList to be disabled before and after edit link.
codes:

<asp:DropDownList ID="DropDownList1" runat="server" Height="30px" Width="190px" SelectedValue='<%# Eval("FAQGroup") %>' Enabled="false" >
    <asp:ListItem Value="Most asked FAQ"></asp:ListItem>
    <asp:ListItem Value="Normal FAQ"></asp:ListItem>
</asp:DropDownList>

aspx.cs

 protected void gvFAQ_RowEditing(object sender, GridViewEditEventArgs e)
    {
         gvFAQ.Columns[3].Visible = true;

         DropDownList DDL= (DropDownList)gvFAQ.Rows[e.NewEditIndex].FindControl("DropDownList1");
         DDL.Enabled = true;

         gvFAQ.EditIndex = e.NewEditIndex;
         bind();
    }

Solution

  • When you call bind at the end of the RowEditing event handler, the GridView is cleared and refilled, and a new DropDownList is created in each row. The control must be enabled after the data is bound, for example in the RowDataBound event handler:

    protected void gvFAQ_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
            ddl.Enabled = e.Row.RowIndex == gvFAQ.EditIndex;
        }
    }