我试图在单击按钮时加载用户控件,但问题是,它在用户控件内的回发时消失了。
这是我加载控件的方式:
private bool IsUserControl
{
get
{
if (ViewState["IsUserControl"] != null)
{
return (bool)ViewState["IsUserControl"];
}
else
{
return false;
}
}
set
{
ViewState["IsUserControl"] = value;
}
}
#region Usercontrols
private void CreateUserControlAllNews()
{
Control featuredProduct = Page.LoadControl("path/usercontrol.ascx");
plh1.Controls.Add(featuredProduct);
}
#endregion
protected void allNewsbtn_Click(object sender, EventArgs e)
{
this.IsUserControl = true;
if(IsUserControl)
CreateUserControlAllNews();
}
最佳答案
您需要在回发页面时重新加载控件。例如,
protected void Page_Load(object sender, EventArgs e)
{
if (IsUserControl)
{
CreateUserControlAllNews();
}
}
private void CreateUserControlAllNews()
{
Control featuredProduct = Page.LoadControl("path/usercontrol.ascx");
featuredProduct.ID = "1234";
plh1.Controls.Add(featuredProduct);
}
关于c# - 在按钮单击、回发问题上动态加载用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14461452/