繁体   English   中英

单击用户控件内的按钮后,将重点放在aspx页面内的用户控件上

[英]focus on a user control inside an aspx page after clicking a button inside the user control

我在aspx页面中有一个用户控件,在aspx页面加载之后,当我单击该用户控件中的某个按钮时,我希望焦点在该按钮单击操作完成并且aspx页面再次加载后回到用户控件。

您需要在用户控件中有一个事件,该事件将允许.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;

最后,单击事件处理程序需要存在,如下所示:

public void Button1_Click(object sender, EventArgs args)
{
    // Set focus here to user control element, text box for example
    ((TextBox)userControl.FindControl("TextBox1")).Focus();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM