//动态生成LABEL
for(int i=0;i<10;i++)
{
Label lb = new Label();
lb.Text = "123";
lb.ID = "lb"+i;
}
//取值时
for(int i=0;i<10;i++)
{
string str = ((Label)(this.FindControl("lb")+i)).Text;
........
}
由于使用母版页,前台ID已变。
后台应该怎么获取?
你这个查找控件的代码有点问题啊。
//取值时问题不出在这儿,
后台设置LB.ID="lb"+i时,前台默认显示的标签ID变为"ct100_ContentPlaceHolderID_lb0",加了母版页的ID。。所以取不到前台的控件。
难道我要去掉母版页里面的ID?
如果你是在C#代码中找id,不用那么麻烦,你用我贴上去的代码试一下。你的代码FindControl("lb"),是根据ID=“lb”找控件,并没有把i加上去。
如果你是在客户端找控件,就用这种方式。
是后台取,我的代码里已经加了i, 我试过你的代码出错和我的一样。。取不到控件。。。
追答你动态生成的Lable,有没有添加入当前的Page页面?
从你贴出的代码中,我并没有看到把Lable添加入当前页面的代码。
第一次贴的只是部分代码。源码中已加入前台PANEL, 下面为前天生成的源码。ID已变
你好。今天测试一下代码,发现我之前的回答基本都没帮上你什么,实在抱歉。
下面两种情况,希望可以解决你遇到的问题。
控件添加后,立刻使用(注:期间没有发生任何回传)
protected void Page_Load(object sender, EventArgs e)