我在面板中动态创建了ListBox,我想从动态创建的列表框中读取所选项目。下面是我用来创建动态列表框的代码。谁能帮助我如何获得动态创建的列表框,然后阅读所选项目。 'protected void GotoReport_Click(object sender,ImageClickEventArgs e) {
foreach (TreeNode tndim in tvCubedef.CheckedNodes)
{
lbFilter.Items.Add(tndim.Text);
}
foreach (ListItem item in lbFilter.Items)
{
item.Selected = true;
}
panFilter.Controls.Clear();
connstr2 = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
conn2.ConnectionString = connstr2;
conn2.Open();
CubeCollection CubeList = conn2.Cubes;
string cb = ddlCubeList.SelectedItem.Text;
foreach (ListItem li in lbFilter.Items)
{
ListBox listb = new ListBox();
ListItem Memlist = new ListItem();
listb.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
listb.Height = 150;
listb.Width = 250;
string Repl1 = li.Value.Replace("[", "");
string Repl2 = Repl1.Replace("]", "");
string[] DimMember = Repl2.Split('.');
foreach (Member dimem in CubeList[cb].Dimensions[DimMember[0]].Hierarchies[DimMember[1]].Levels[DimMember[2]].GetMembers())
{
Memlist.Text = dimem.Name;
listb.Items.Add(Memlist);
panFilter.Controls.Add(listb);
}
}
} '
答案 0 :(得分:0)
您需要动态添加事件处理程序:
listb.SelectedIndexChanged += new EventHandler(listb_SelectedIndexChanged);
当然,您还需要提供此方法:
protected void listb_SelectedIndexChanged(Object sender, EventArgs e)
{
ListBox listb = (ListBox) sender;
}
您是否最迟在ListBox
中以及之前的page_load
重新创建了ID
每次回发(您应该)?