无法添加自定义控件到的UpdatePanel
问题描述:
我的代码:出现无法添加自定义控件到的UpdatePanel
protected void Button1_Click(object sender, EventArgs e)
{
CustomControl myControl = new CustomControl();
UpdatePanel1.ContentTemplateContainer.Controls.Add(myControl);
Button btn = new Button();
UpdatePanel1.ContentTemplateContainer.Controls.Add(btn);
}
点击后新按钮,但我的控制没有。 CustomControl如果罚款(?),因为我可以将其添加到.aspx文件中,并且它没有任何问题。
我错了什么?
btw。没有错误或警告,它只是不添加自定义控件到HTML页面。
答
我发现了一个基于相关主题的讨论的解决方案。
现在是:
CustomControl myControl = new CustomControl();
应该是:
string controlPath = @"~/Controls/CustomControl.ascx";
CustomControl myControl = (CustomControl)LoadControl(controlPath);