在页面上添加了一个panel,在panel上放了一个dropdownlist,
使用this.panel1.FindControl(“id”)却返回NULL,
看页面的source,id的确就是控件的ID,完全相同,虽然使用了母板。
控件的parent也的确是panel1,
为什么返回NULL,怎么也想不通,还请高手指点?

解决方案 »

  1.   

    Panel1.FindControl(string.Format("id{0}", ""))
      

  2.   

     DropDownList ddl_ID=(DropDownList)(this.panel1.FindControl(“id”));
      

  3.   

    FindControl中的ID是指服务器控件的ID,不是生成到html后的ClientID
      

  4.   


    话说楼主都没说是在web上还是winform上面,你还真会猜测啊。同样楼主问问题没水平。
      

  5.   


    winform里有DropDownList么?
    System.Windows.Forms.Control类有FindControl方法么?
      

  6.   

    再加上页面的source,母版
    难道你还不能确定是winform还是web form?
      

  7.   

    如果是使用了模板请
    string clientid=dr.ClientId;
    this.panel1.FindControl(“clientid”)
      

  8.   

    如果还是不行的,请在浏览器中运行此页面,然后右键查看源代码,找到此控件的实际id,copy到this.panel1.FindControl(“id”)
    测试过很多次,此值不会改变!
      

  9.   

    this.panel1.FindControl(“id”)
    (“id”)里的id指的是DropDownList控件的Id
    一定能找到。
      

  10.   

    9楼的不要误导人,下面的例子可以说明问题,当然我不知道楼主的页面布局是怎样的:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Master.aspx.cs" Inherits="WebApplication1.Master"
        MasterPageFile="~/Site1.Master" %><asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server" ID="Content">
        <asp:Panel ID="Panel1" runat="server">
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
        </asp:Panel>
    </asp:Content>
            protected void Page_Load(object sender, EventArgs e)
            {
                DropDownList ddl = this.Panel1.FindControl("DropDownList1") as DropDownList;
                Response.Write(ddl==null?"null":ddl.ClientID);
                Response.Write("<br />");
                ddl = this.Panel1.FindControl(DropDownList1.ClientID) as DropDownList;
                Response.Write(ddl == null);
            }
      

  11.   

    因为我是动态生成的,在.aspx页面用
    for循环生成的,用findcontrol只能找服务器控件。
      

  12.   

    如果使用母版或Gridview的时候,客户端的空间ID号会发生改变,但改变是有规则的,
    就那你的母版来说的话,比如一个ID:TextBox1的文本框,你的母版ID:ContentPlaceHolder1,
    那么如果想使用FindControl("ID")的话,这个就会变为ID:ct100_ContentPlaceHolder1_TextBox1,
    你可以使用这个ID尝试FindControl,应该可以抓取到,如果还是不行的话,你可以加断点获取一下控件的ClientID,
    用得到的ID号再FindControl~~