首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么将字符串得到控件id 的后续有关问题The name 'dd1' does not exist in the current context

2012-09-19 
如何将字符串得到控件id 的后续问题The name dd1 does not exist in the current context大家帮我看,为

如何将字符串得到控件id 的后续问题The name 'dd1' does not exist in the current context
大家帮我看,为什么会有这个问题:The name 'dd1' does not exist in the current context


Label[] myLabel8=new Label[5];
myLabel8[0]=label1;
myLabel8[1]=label2;
myLabel8[2]=label3;
myLabel8[3]=label4;
myLabel8[4]=label5;
TextBox[] td0=new TextBox[5];
td0[0]=textbox1;
td0[1]=textbox2;
td0[2]=textbox3;
td0[3]=textbox4;
td0[4]=textbox5;

for(int i=0;i<5;i++){
if (myLabel8[i].Text.IndexOf("申请人")>-1){
String a="dropdownlist";
String b=(i+1).ToString();
DropDownList ddl=this.FindControl(a+b) as DropDownList; 

td0[i].Text=dd1.SelectedItem.ToString().Trim();
}
}

[解决办法]
ddl没有添加到页面中,this.Form.Control.Add(ddl);
具体添加在哪个控件下你自己决定。。。
[解决办法]
编译不过去吧?我也没有找到dd1 我看到了ddl 数字1个字母L有点区别
[解决办法]
dropdownlist1
dropdownlist2
dropdownlist3
dropdownlist4
dropdownlist5
在页面中没找到,所以
FindControl('dropdownlist1')返回结果为NULL。
而你又把NULL转换成DropDownList,所以会抛异常。

建议把DropDownList ddl=this.FindControl(a+b) as DropDownList;
拆成如下几句:

C# code
Control ctrl = this.FindControl(a+b);if(ctrl!=null && ctrl.GetType()==Typeof(DropDownList)){   DropDownList ddl = ctrl as DropDownList;   //TODO: Something} 

热点排行