按钮单击事件未在用户控件中触发,在另一个用户控件中动态创建
Button click event not firing in user control, created dynamically in another user control
我有可视化 Web 部件和两个用户控件。
在可视化 Web 部件的 Page_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)(并且有效!):
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
.
请帮帮我!
必须在每次页面加载时重新创建动态生成的控件,其中包括 PostBack。因为第二个用户控件仅在单击按钮时加载,所以在执行另一个 PostBack 时它会消失。您必须跟踪是否已创建 UserContol2
,如果已创建,请将其重新加载到父级的 Page_Load 中。在此片段中,我使用 Session
来跟踪 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);
}
}
我有可视化 Web 部件和两个用户控件。
在可视化 Web 部件的 Page_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)(并且有效!):
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
.
请帮帮我!
必须在每次页面加载时重新创建动态生成的控件,其中包括 PostBack。因为第二个用户控件仅在单击按钮时加载,所以在执行另一个 PostBack 时它会消失。您必须跟踪是否已创建 UserContol2
,如果已创建,请将其重新加载到父级的 Page_Load 中。在此片段中,我使用 Session
来跟踪 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);
}
}