当前位置: 代码迷 >> ASP.NET >> 怎么将字符串得到控件id 的后续有关问题The name 'dd1' does not exist in the current context
  详细解决方案

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

热度:3844   发布时间:2013-02-25 00:00:00.0
如何将字符串得到控件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}
  相关解决方案