我正在尝试在按钮点击时加载用户控件,但问题是,它会在用户控件内回发时消失。
这就是我加载控件的方式:
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();
}
答案 0 :(得分:4)
您需要在回发页面时重新加载控件。例如,
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);
}
答案 1 :(得分:1)