我在ASP.NET中创建了动态列表框(4到10)。 我的问题是,我如何使用c#找到动态创建的列表框?
感谢
当然......我感谢你的帮助。下面的代码我用于创建动态LB
protected void btndyfilter_Click(object sender, EventArgs e)
{
int numberOfListBox = lbFilter.GetSelectedIndices().Length;
string lbname = lbFilter.SelectedValue;
for (int i = 0; i < numberOfListBox; i++)
{
ListBox listb = new ListBox();
ListItem lItem = new ListItem();
listb.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
listb.Height = 150;
listb.Width = 200;
lItem.Value = i.ToString();
lItem.Text = lbname;
listb.Items.Add(lItem);
panFilter.Controls.Add(listb);
//once we created the LB dynamically i need to populate each LB with the corresponding values
connstr2 = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
conn2.ConnectionString = connstr2;
conn2.Open();
CubeCollection CubeList = conn2.Cubes;
string cb = ddlCubeList.SelectedItem.Text;
//need to remove the Hardcoded Code
foreach (Member dimem in CubeList[cb].Dimensions["Date"].Hierarchies["Calendar Date"].Levels["Date"].GetMembers())
{
ListItem Memlist = new ListItem();
Memlist.Text = dimem.UniqueName;
lbFilter.Items.Add(Memlist);
}
}
panFilter.Visible = true;
panCubeDef.Visible = true;
}
所以这将创建LB我相信:))和评论代码内部我需要用来填充每个LB项目..也许它有点硬编码,我需要删除。所以我填充了所有动态LB,然后所有LB中的所选项目将进入我的MDX查询中的where子句..希望我没有让您感到困惑
答案 0 :(得分:2)
有两种方法可以将动态控制细节存储到字典中,或者只是想要使用某些代码来查找它
Control GetControlByName(string Name)
{
foreach(Control c in this.Controls)
if(c.Name == Name)
return c;
return null;
}
答案 1 :(得分:0)
在动态生成ListBox时,将ListBox ID指定为:
lstBoxNo1
,lstBoxNo2
。 lstBoxNo3
等1,2,3(否)将来自计数。
像
int count = 1;
生成列表框控件
listboxid=lastBoxNo+count;
count++
否则使用
http://stackoverflow.com/questions/3731007/using-findcontrol-to-find-control
使用此链接了解findcontrol。
答案 2 :(得分:0)
您不会发现动态控件的要点。
在1的情况下,您最好在页面上保留一个保留该创建的变量。
在2的情况下,当你回发时,你需要在渲染时以某种方式存储控件的创建。一个好的地方是将这些信息保存在viewstate上。
您也可以在帖子上回来,只是为了检查您是否从使用从1开始的序列号命名的控件中有任何回发值,例如,您开始查看是否从ControlName_1回发,然后是ControlName_2,当你没有找到任何其他价值时,你就会结束。