Share via


How to set focus on the asp.net textbox in AxPopup form?

To set the focus include this method in your page that is opened in the popup window and call the method without control name in the page load event.

For example to set the focus on txtCustAccount

 

protected void Page_Load(object sender, EventArgs e)

    {

       

        SetFocus(txtCustAccount);

    }

public static void SetFocus(Control control)

    {

        StringBuilder sb = new StringBuilder();

        sb.Append("<script language='JavaScript'>");

        sb.Append("function SetFocus(){document.");

        Control p = control.Parent;

        while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;

        sb.Append(p.ClientID);

        sb.Append("['");

        sb.Append(control.UniqueID);

        sb.Append("'].focus();}");

       

        sb.Append("window.onload = SetFocus;</script>\r\n");

        control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());

    }

Comments

  • Anonymous
    April 28, 2010
    Hello, I have tried this, but it does not work. I still do not have focus on my field when I open a page. If I use TAB once, I will be in the right field but not before?? Does anyone have an Idea? Lotte