Gridview Itemtemplate DropDownList已启用

时间:2016-07-28 15:21:21

标签: c# asp.net gridview itemtemplate dropdownbox

我希望DropDownList被禁用,并且只有在点击Gridview上的修改链接后启用它。截至目前,它显示在编辑链接之前和之后禁用DropDownList
代码:

<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();
    }

1 个答案:

答案 0 :(得分:4)

当您在bind事件处理程序的末尾调用RowEditing时,GridView将被清除并重新填充,并在每一行中创建一个新的DropDownList。绑定数据后必须启用控件,例如在RowDataBound事件处理程序中:

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;
    }
}