<asp:GridView ID="grdUsers" CssClass="" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="usr_id" EnableModelValidation="True"
AllowPaging="True" PageSize="10" BorderWidth="0"
OnRowCommand="grdUsers_RowCommand" OnRowDeleting="grdUsers_RowDeleting" OnRowDataBound="grdUsers_OnRowDataBound" OnRowEditing="grdUsers_RowEditing" OnRowUpdating="grdUsers_RowUpdating" OnRowCancelingEdit="grdUsers_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lbl_country" runat="server" Text='<%#Eval("usr_country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_countries" runat="server" CssClass="" AppendDataBoundItems="false" AutoPostBack="false"
DataValueField="cou_id" DataTextField="cou_name">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
First I tried to access DropDownList in RowEditing (RowEditing occurs before RowDataBound):
protected void grdUsers_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
grdUsers.EditIndex = e.NewEditIndex;//NewEditIndex property used to determine the index of the row being edited.
GridViewRow editingRow = grdUsers.Rows[e.NewEditIndex];
DropDownList ddlCountries0 = (DropDownList)(editingRow.FindControl("ddl_countries"));
DropDownList ddlCountries1 = (DropDownList)(grdUsers.Rows[grdUsers.EditIndex].FindControl("ddl_countries"));
DropDownList ddlCountries2 = (DropDownList)grdUsers.Rows[e.NewEditIndex].FindControl("ddl_countries");
DropDownList ddlCountries = ((DropDownList)grdUsers.Rows[e.NewEditIndex].Cells[4].FindControl("ddl_countries"));
if (ddlCountries != null)
{
LoadCountriesDropdownValue(ddlCountries);
}
}
But ddlCountries is always null. So I tried RowDataBound event (below are several different tries) but ddlCountries is always null as well:
protected void grdUsers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.TableSection = TableRowSection.TableHeader;
}
if (e.Row.RowType == DataControlRowType.DataRow && grdUsers.EditIndex == e.Row.RowIndex)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl_countries");
}
GridViewRow row = e.Row;
DropDownList ddl = (DropDownList)row.FindControl("ddl_countries");
if ((row.RowType == DataControlRowType.DataRow) && ((row.RowState & DataControlRowState.Edit) > 0))
{
DropDownList ddl2 = (DropDownList)row.FindControl("ddl_countries");
}
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("ddl_countries");
}
if (e.Row.RowType == DataControlRowType.DataRow)//skip header row
{
//if (grdUsers.EditIndex == e.Row.RowIndex)
//if ((e.Row.RowState & DataControlRowState.Edit) > 0)
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
//DropDownList ddlCountries = grdUsers.Rows[e.RowIndex].FindControl("ddl_countries") as DropDownList;
DropDownList ddlCountries = (DropDownList)e.Row.Cells[4].FindControl("ddl_countries");
LoadCountriesDropdownValue(ddlCountries);
}
}
}
Sorry in case it was asked before but after hours of searching I always have ddlCountries as null in code-behind.
question from:
https://stackoverflow.com/questions/65646375/cant-access-dropdownlist-inside-edititemtemplate-in-gridview-its-always-null 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…