我有可视化 Web 部件和两个用户控件。在Page_Load()
我动态创建的可视 Web 部件上userControl1
:
protected void Page_Load(object sender, EventArgs e)
{
UserControl1 userControl = Page.LoadControl(userControl1Path) as UserControl1;
userControl.ID = "UserControl1";
this.Controls.Clear();
this.Controls.Add(userControl);
}
在 UserControl1 我有一个按钮,它加载第二个用户控件(UserControl2)(它可以工作!):
protected void GoToUserControl2_Click(object sender, EventArgs e)
{
UserContol2 userControl = Page.LoadControl(userControl2Path) as UserContol2;
userControl.ID = "UserContol2";
this.Controls.Clear();
this.Controls.Add(userControl);
}
UserControl2
也有一个按钮,但是当我点击它时 - 点击事件不会触发。而不是它,按钮单击执行重定向到UserControl1
. 即使按钮没有任何事件 - 它也会重定向到UserControl1
.
请帮帮我!
动态生成的控件必须在每次页面加载时重新创建,其中包括回发。因为第二个用户控件仅在单击按钮时加载,所以在执行另一个回发时它会消失。您必须跟踪是否UserContol2
已创建,如果已创建,请将其重新加载到父级的 Page_Load 中。在这个片段中,我使用 aSession
来跟踪UserContol2
.
在按钮单击方法中设置会话
protected void GoToUserControl2_Click(object sender, EventArgs e)
{
//rest of the code
Session["uc2_open"] = true;
}
并检查 Page_load 会话是否存在,如果存在,则创建第二个用户控件。
protected void Page_Load(object sender, EventArgs e)
{
UserControl1 userControl = Page.LoadControl(userControl1Path) as UserControl1;
userControl.ID = "UserControl1";
this.Controls.Clear();
this.Controls.Add(userControl);
if (Session["uc2_open"] != null)
{
UserContol2 userControl = Page.LoadControl(userControl2Path) as UserContol2;
userControl.ID = "UserContol2";
this.Controls.Clear();
this.Controls.Add(userControl);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句