在页面上添加了一个panel,在panel上放了一个dropdownlist,
使用this.panel1.FindControl(“id”)却返回NULL,
看页面的source,id的确就是控件的ID,完全相同,虽然使用了母板。
控件的parent也的确是panel1,
为什么返回NULL,怎么也想不通,还请高手指点?
使用this.panel1.FindControl(“id”)却返回NULL,
看页面的source,id的确就是控件的ID,完全相同,虽然使用了母板。
控件的parent也的确是panel1,
为什么返回NULL,怎么也想不通,还请高手指点?
话说楼主都没说是在web上还是winform上面,你还真会猜测啊。同样楼主问问题没水平。
winform里有DropDownList么?
System.Windows.Forms.Control类有FindControl方法么?
难道你还不能确定是winform还是web form?
string clientid=dr.ClientId;
this.panel1.FindControl(“clientid”)
测试过很多次,此值不会改变!
(“id”)里的id指的是DropDownList控件的Id
一定能找到。
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);
}
for循环生成的,用findcontrol只能找服务器控件。
就那你的母版来说的话,比如一个ID:TextBox1的文本框,你的母版ID:ContentPlaceHolder1,
那么如果想使用FindControl("ID")的话,这个就会变为ID:ct100_ContentPlaceHolder1_TextBox1,
你可以使用这个ID尝试FindControl,应该可以抓取到,如果还是不行的话,你可以加断点获取一下控件的ClientID,
用得到的ID号再FindControl~~