我在 aspx 页面中有一个用户控件,在 aspx 页面加载后,当我单击该用户控件中的某个按钮时,我希望在该按钮单击操作完成并再次加载 aspx 页面后焦点回到用户控件。
问问题
413 次
1 回答
0
您需要在用户控件中有一个事件,该事件将允许 .aspx 页面订阅该事件,以便它可以在表单回发后将焦点设置到用户控件中的元素,如下所示:
public class UserControlClass
{
// Define event that will be raised by user control to anyone interested in handling the event
public event UC_Button1ClickEventHandler UC_Button1Click;
public delegate void UC_Button1ClickEventHandler();
// Mechanism to allow event to be raised by user control
private void Button1_Click(System.Object sender, System.EventArgs e)
{
if (UC_Button1Click != null)
{
UC_Button1Click();
}
}
}
现在在您的 .aspx 页面中,您需要订阅用户控件中的事件并说明实际处理事件的方法,如下所示:
userControl1.UC_Button1Click += Button1_Click;
最后,click 事件处理程序需要存在,如下所示:
public void Button1_Click(object sender, EventArgs args)
{
// Set focus here to user control element, text box for example
((TextBox)userControl.FindControl("TextBox1")).Focus();
}
于 2013-10-26T02:06:07.410 回答