Gridview row editing - dynamic binding to a DropDownList

后端 未结 6 1617
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 01:33

I\'m trying to get an ASP.NET 3.5 GridView to show a selected value as string when being displayed, and to show a DropDownList to allow me to pick a value from a given list

6条回答
  •  北海茫月
    2020-12-03 02:05

    Quite easy... You're doing it wrong, because by that event the control is not there:

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && 
            (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        { 
            // Here you will get the Control you need like:
            DropDownList dl = (DropDownList)e.Row.FindControl("ddlPBXTypeNS");
        }
    }
    

    That is, it will only be valid for a DataRow (the actually row with data), and if it's in Edit mode... because you only edit one row at a time. The e.Row.FindControl("ddlPBXTypeNS") will only find the control that you want.

提交回复
热议问题