上按一下按钮动态加载用户控件,回发问题 [英] Dynamically load user controls on button click, postback issue
本文介绍了上按一下按钮动态加载用户控件,回发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图加载按钮点击用户控件,但问题是,它消失在回传用户控件中。
这是我如何加载控件:
私人布尔IsUserControl
{
得到
{
如果(的ViewState [IsUserControl]!= NULL)
{
回报(布尔)的ViewState [IsUserControl];
}
其他
{
返回false;
}
}
组
{
的ViewState [IsUserControl] =值;
}
}
#区域用户控件
私人无效CreateUserControlAllNews()
{
控制featuredProduct = Page.LoadControl(路径/ usercontrol.ascx);
plh1.Controls.Add(featuredProduct);
}#endregion
保护无效allNewsbtn_Click(对象发件人,EventArgs的发送)
{ this.IsUserControl = TRUE;
如果(IsUserControl)
CreateUserControlAllNews();
}
解决方案
您需要重新加载控制页面时后回来。例如,
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(IsUserControl)
{
CreateUserControlAllNews();
}
}私人无效CreateUserControlAllNews()
{
控制featuredProduct = Page.LoadControl(路径/ usercontrol.ascx);
featuredProduct.ID =1234;
plh1.Controls.Add(featuredProduct);
}
I'm trying to load usercontrols on button click, but problem is that, it disappears on postback inside user control.
this is how i load controls:
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();
}
解决方案
You need to reload the control when the page is post back. For example,
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);
}
这篇关于上按一下按钮动态加载用户控件,回发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文