C# 如何在ContentPlaceholder和placeholder中找到控件?
我正在向ContentPlaceHolder中的占位符动态添加控件C# 如何在ContentPlaceholder和placeholder中找到控件?,c#,asp.net,master-pages,C#,Asp.net,Master Pages,我正在向ContentPlaceHolder中的占位符动态添加控件 var t = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1"); var t1 = (PlaceHolder)mpContentPlaceHolder.FindControl("PlaceHolderName"); var t2 = (DropDownList)t1.FindControl("ControlID"); 看起来我遗漏了一些东西,因为t2
var t = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
var t1 = (PlaceHolder)mpContentPlaceHolder.FindControl("PlaceHolderName");
var t2 = (DropDownList)t1.FindControl("ControlID");
看起来我遗漏了一些东西,因为t2总是空的如果您动态添加控件,并且在回发后尝试查找控件,您将找不到它们。如果您要查找母版页的控件,则可以找到如下内容:
Label l = Master.FindControl("label1") as Label;
在您从contentplaceholder查找控件的情况下,您可以找到如下控件:
TextBox TB=
Master.FindControl("ContentPlaceHolder1").FindControl("textbox1") as
TextBox;
我发现gridview使用以下方法:
GridView gv =(GridView)Master.FindControl("ContentPlaceHolder1").FindControl("gvRD");
我这样做的按钮点击,它的工作,如果我不使用ContentPlaceHolder只是占位符。但是我有一个内容页,所以我需要使用它。在您的代码中,什么是mpContentPlaceHolder。您已将ContentPlaceHlder1分配给变量t。我尝试了类似于“Master.FindControl(“contentplaceholder 1”).FindControl(“placeholder name”).FindControl(“ControlID”)”的东西,但它没有任何问题。mpContentPlaceHolder是否应该是
t
?另外,请确保没有在(!IsPostBack)
块中添加控件。