当前位置:编程学习 > C#/ASP.NET >>

FindControl不到dropdownlist

页面有n个dropdownlist,后台循环向每个dropdownlist赋默认值。

代码如下:
          string[] sArray = allpcatno.Split(new Char[] { ' ', ',' });
            for(int i=0;i<sArray.Length;i++)
            {
                if (sArray[i] != "")
                {
                    dname = "DropDownList" + (i + 1);
                    DropDownList lddl = (DropDownList)FindControl(dname);
                    if (lddl != null)
                    {
                        lddl.Items.FindByValue(sArray[i]).Selected = true;
                    }
                }
            }

但是使用findcontrol(dname)获取不到,如果dname是控件的clientid的话就能findcontrol到,但是又如何取到clientid呢? 谢谢了! dropdownlist --------------------编程问答-------------------- id是唯一标识的 而name不是 如果用name就可能找到控件集合 用ID就是唯一找到的 --------------------编程问答-------------------- 你的dropdownlist 是嵌套在其它控件里的么?  没有的话,为什么不直接给值,有的话在其它控件加载的时候查找dropdownlist  --------------------编程问答--------------------
引用 2 楼 shiyong7682719 的回复:
你的dropdownlist 是嵌套在其它控件里的么?  没有的话,为什么不直接给值,有的话在其它控件加载的时候查找dropdownlist 


对哦,用了母板页,找到了
ContentPlaceHolder ContentPlaceHolderSql = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
DropDownList lddl = (DropDownList)ContentPlaceHolderSql.FindControl(dname);

但是又出现新的问题,执行
 lddl.Items.FindByValue(sArray[i]).Selected = true;
出现错误Object reference not set to an instance of an object. --------------------编程问答-------------------- 未将对象引用设置到对象的实例...  sArray[i]你这玩意有没有值?  自己调式看看 --------------------编程问答-------------------- 即使是lddl.Items.FindByValue("13092520").Selected = true;也报错。
而sArray[i]的值就是13092520

另外,如果使用name
DropDownList d = (DropDownList)FindControl("ctl00$ContentPlaceHolder1$DropDownList1");
d.Items.FindByValue("13092520").Selected = true;
是没有问题的,
但是使用id
DropDownList d = (DropDownList)FindControl("ctl00_ContentPlaceHolder1_DropDownList1");
d.Items.FindByValue("13092520").Selected = true;
也一样报错

到底是什么问题啊?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,