C# 在另一个用户控件中动态创建的用户控件中未触发的按钮单击事件
我有可视化web部件和两个用户控件。 在visual web part i的C# 在另一个用户控件中动态创建的用户控件中未触发的按钮单击事件,c#,asp.net,sharepoint,user-controls,web-parts,C#,Asp.net,Sharepoint,User Controls,Web Parts,我有可视化web部件和两个用户控件。 在visual web part i的Page\u Load()上动态创建userControl1: protected void Page_Load(object sender, EventArgs e) { UserControl1 userControl = Page.LoadControl(userControl1Path) as UserControl1; userControl.ID = "UserControl1"; this.Co
Page\u Load()
上动态创建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)(它可以工作!):
UserControl2
也有一个按钮,但当我点击它时,点击事件不触发。而不是单击按钮执行重定向到UserControl1
。
即使按钮没有任何事件,它也会重定向到UserControl1
请帮帮我 每次加载页面时都必须重新创建动态生成的控件,这包括回发。由于第二个用户控件仅在单击按钮时加载,因此在执行另一次回发时它将消失。如果已创建
UserContol2
,则必须跟踪,如果已创建,请在父级的页面加载中重新加载。在这个片段中,我使用了一个会话
来跟踪UserContol2
的打开
在按钮单击方法中设置会话
protected void GoToUserControl2_Click(object sender, EventArgs e)
{
//rest of the code
Session["uc2_open"] = true;
}
并在页面上检查会话是否存在,如果存在,则创建第二个用户控件
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);
}
}
在页面加载中放置一个断点,您会注意到每次有回发时都会执行页面加载函数,我怀疑您的页面加载清除了控件2并使用控件1的事件
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);
}
}