Je le code suivant dans la méthode page_load du fichier cs:difficulté à ajouter un eventhandler dynamiquement créé dropdownlist
DataSet data = new DataSet();
DataTable parent = new DataTable();
DataTable child = new DataTable();
//parse the xml here
parent.TableName = "Parent";
parent.Columns.Add("ID", typeof(int));
parent.Columns.Add("Name", typeof(string));
child.TableName = "ParentChild";
child.Columns.Add("ID", typeof(int));
child.Columns.Add("ParentID", typeof(int));
child.Columns.Add("ChildName", typeof(string));
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(objDemo.ChildsList);
XmlNodeList objNL = xmlDoc.GetElementsByTagName("option");
int parentIndex = 0;
foreach (XmlNode objSingleNode in objNL)
{
parent.Rows.Add(parentIndex, objSingleNode.Attributes.GetNamedItem("name").InnerText);
XmlDocument xmlChildren = new XmlDocument();
xmlChildren.LoadXml(objSingleNode.InnerXml);
XmlNodeList objCL = xmlChildren.GetElementsByTagName("child");
int childIndex = 0;
foreach (XmlNode objSingleChild in objCL)
{
child.Rows.Add(childIndex, parentIndex, objSingleChild.InnerText);
childIndex++;
}
parentIndex++;
}
data.Tables.Add(parent);
data.Tables.Add(child);
data.Relations.Add("ParentChild", parent.Columns["ID"], child.Columns["ParentID"]);
bindingSource1 = new BindingSource();
bindingSource1.DataSource = data;
bindingSource1.DataMember = "Parent";
bindingSource2 = new BindingSource();
bindingSource2.DataSource = bindingSource1;
bindingSource2.DataMember = "ParentChild";
DropDownList lstList1 = new DropDownList();
lstList1.ID = "ParentList";
lstList1.SelectedIndexChanged += lstList1_SelectedIndexChanged;
lstList1.DataSource = bindingSource1;
lstList1.DataTextField = "Name";
lstList1.DataValueField = "Name";
lstList1.AutoPostBack = true;
lstList1.DataBind();
DropDownList lstList2 = new DropDownList();
lstList2.ID = "ChildList";
lstList2.DataSource = bindingSource2;
lstList2.DataTextField = "ChildName";
lstList2.DataValueField = "ChildName";
lstList2.DataBind();
this.dynamicControl.Controls.Add(
new LiteralControl(
"<tr><td width='220px' class='contentBoxLabel'>" + sbDisplayLabel + " - Country" + "</td><td>"));
this.dynamicControl.Controls.Add(lstList1);
this.dynamicControl.Controls.Add(new LiteralControl("</td></tr>\n"));
this.dynamicControl.Controls.Add(
new LiteralControl(
"<tr><td width='220px' class='contentBoxLabel'>" + sbDisplayLabel + "</td><td>"));
this.dynamicControl.Controls.Add(lstList2);
Le SelectedIndexChanged pour lstList1 ne fonctionne pas, ce qui signifie la fonction lstList1_SelectedIndexChanged ne soit jamais atteint, faire Je dois ajouter ça ailleurs? J'ai essayé de l'ajouter dans la partie IsPostBack du Page_load, mais j'ai du mal à trouver le contrôle. J'ai l'impression que j'ai besoin d'un gestionnaire d'événements pour mettre à jour la deuxième liste déroulante quand la première a été sélectionnée, est-ce exact?
Bienvenue sur SO. Où est votre gestionnaire? Que signifie "ne pas travailler"? Veuillez lire ceci avant de poster plus à SO: https://stackoverflow.com/help/mcve –