J'ai un Multiformview dans ma page aspx comme:FindControl d'un FormView qui est à l'intérieur d'un contrôle utilisateur
<data:MultiFormView ID="mFormView1" DataKeyNames="Id" runat="server" DataSourceID="DataSource">
<EditItemTemplate>
<ucl:myControl ID="myControl1" runat="server" />
</EditItemTemplate>
Et à l'intérieur de mon contrôle utilisateur 'MyControl' Je adropdownlist comme ci-dessous:
<asp:FormView ID="FormView1" runat="server" OnItemCreated="FormView1_ItemCreated">
<ItemTemplate>
<table border="0" cellpadding="3" cellspacing="1">
<tr>
<td>
<asp:DropDownList ID="ddlType" runat="server" Width="154px" />
</td>
</tr>
Alors, quand je l'ai essayé d'accéder à ce d ropdownlist dans mon fichier ascx.cs il me donne une erreur de référence nulle.
J'ai essayé suivante:
protected void FormView1_ItemCreated(Object sender, EventArgs e)
{
DropDownList ddlType= FormView1.FindControl("ddlType") as DropDownList;
}
ET
DropDownList ddlType= (DropDownList)FormView1.FindControl("ddlType");
ET: A l'intérieur Databound aussi. Rien ne fonctionne.
EDIT:
Je ne vérifiait pas que Formview1.Row est nulle ou non. Voici la solution:
protected void FormView1_DataBound(object sender, EventArgs e)
{
DropDownList ddlType = null;
if (FormView1.Row != null)
{
ddlType = (DropDownList)FormView1.Row.FindControl("ddlType");
}
}