我在cs文件的page_load方法中有以下代码:
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);
lstList1的SelectedIndexChanged不起作用,意味着永远不会达到函数lstList1_SelectedIndexChanged,我是否需要在其他地方添加?我已经尝试在Page_load的IsPostBack部分添加它,但是我找不到控件。我的印象是我需要有一个事件处理程序来更新第二个下拉列表,当第一个被选中时,这是正确的吗?
答案 0 :(得分:0)
我认为您注册处理程序的方式存在问题 以这种方式尝试事件:
lstList1.SelectedIndexChanged += new EventHandler(lstList1_SelectedIndexChanged);
lstList1.AutoPostBack = true;
void lstList1_SelectedIndexChanged(object sender, EventArgs e)
{
//your code
}