简体   繁体   English

如何在 ASCX 用户控件中设置控件焦点?

[英]How to set control focus in a ASCX user control ?

How to set control focus on Asp.Net user control (ascx file)?如何在 Asp.Net 用户控件(ascx 文件)上设置控件焦点?

Property example: 属性示例:

CS file for the ASCX: ASCX的CS文件:

public partial class MyAscxControl : System.Web.UI.UserControl
{
    // add a property to the ascx control  
    public WebControl MyTextbox 
    { 
        get
        {
            return txtMyTextbox; // the control's id on your ASCX page
        } 
    }
}

CS file for the page that contains the ASCX control: 包含ASCX控件的页面的CS文件:

protected void Page_Load(object sender, EventArgs e)
{
    yourAscxControlId.MyTextBox.Focus();
}

Expose a property / control from user control. 从用户控件公开一个属性/控件。 Access it on parent page and set call .Focus() function. 在父页面上访问它并设置调用.Focus()函数。

eg 例如

((TextBox)userControl.FindControl("TextBox1")).Focus();

或只是TextBox1.Focus()(如果您想在后面的控制代码中使用它):)

如果您想从客户端...

$("#id").focus();

I was the same problem creating an User Control ascx.我在创建用户控件 ascx 时遇到了同样的问题。 My original code was in the page and worked properly, but when I cut the code to use it since my User Control, the property SetFocus("...") it's not available to use it in the same way.我的原始代码在页面中并且工作正常,但是当我从我的用户控件中剪切代码以使用它时,属性 SetFocus("...") 无法以相同的方式使用它。

SetFocus("MyTextBox")

I needed to reference the current page to find the TextBox element:我需要引用当前页面来找到 TextBox 元素:

MyBase.Page.SetFocus("MyTextBox")

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

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