简体   繁体   中英

How to set Onfocus on a text box (Inside a Modal Popup) when a button is clicked in asp.net website

I have a text box inside the modal popup. when a button is clicked the pop up is shown on the screen, I want to set focus on the text box shown below. I tried txt_search.focus(); on page load it didnt work. how can I do it.

<asp:TextBox ID="Txt_search" runat="server">Search..</asp:TextBox>

you can do it like this .. and change this depends upon your requirement

protected void Page_Load(object sender, EventArgs e)
{
      if (!Page.IsPostBack)
      {
            if (!this.ClientScript.IsStartupScriptRegistered("startup"))
            {
                  StringBuilder sb = new StringBuilder();
                  sb.Append("&lt;script type='text/javascript'>");
                  sb.Append("Sys.Application.add_load(modalSetup);");
                  sb.Append("function modalSetup() {");
                  sb.Append(String.Format("var modalPopup = $find('{0}');", popupEntry.BehaviorID));
                  sb.Append("modalPopup.add_shown(SetFocusOnControl); }");
                  sb.Append("function SetFocusOnControl() {");
                  sb.Append(String.Format("var textBox1 = $get('{0}');", txtValue.ClientID));
                  sb.Append("textBox1.focus();}");
                  sb.Append("&lt;/script>");
                  Page.ClientScript.RegisterStartupScript(Page.GetType(), "startup", sb.ToString());
             }
      }
}

take a look at here for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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