Page view counter

ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

Rate It (1)

Last post 05-27-2009 5:21 AM by gargi tripathi. 17 replies.

Sort Posts:

  • ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    05-26-2007, 7:44 PM
    • Loading...
    • mepurathu
    • Joined on 05-16-2006, 8:31 PM
    • Posts 7
    • Points 25

    Hi; I am using the released version of AJAX Controls and I am working on a Web Application with a Masterpage. 
    What I would like to do is, I have two Radiobuttons and two TextBoxes inside an update panel; When the user clicks the RadioButton1, I would like to set the focus to TextBox1 and if the user clicks the RadioButton2, I would like to set the focus to the TextBox2. I have googled for it and found couple of similar solutions that works with the beta AJAX assembly "Microsoft.Web.Atlas" but it doesn't work with the ASP.Net AJAX which I am using in my current project.

    The following code works just fine when I use the Microsoft.Web.Atlas so please let me know if anyone knows a solution that works with the released version of AJAX Controls; Thanks in advance.

    //***************************************************************************************

        //Code behind
    
        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            SetMyFocus(ref TextBox1);
            TextBox1.Focus();
        }
    
        private void SetMyFocus(ref TextBox txtBox)
        {
            string focusString = "setTimeout(\"$('" + txtBox.ClientID + "').focus(); \", 100);";
            ClientScript.RegisterStartupScript(this.GetType(), "focusString", focusString, true);
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            SetMyFocus(ref TextBox2);
            TextBox2.Focus();
        }
     
    //***************************************************************************************
     The following code is from the source view of the page
     
    <%@ Page Language="C#" MasterPageFile="~/Test.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" Title="Untitled Page" %>
    <%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="CPHTest" Runat="Server">
        
       <cc1:ScriptManager ID="ScriptManager2" runat="server" EnablePartialRendering="true">
       </cc1:ScriptManager>
        <cc1:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
              <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" OnCheckedChanged="RadioButton1_CheckedChanged" GroupName="Group1" /><br />
              <asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="Group1" OnCheckedChanged="RadioButton2_CheckedChanged" /><br />
              <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
              <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
              <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
               
          </ContentTemplate>
        </cc1:UpdatePanel>
    </asp:Content>
     
  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    05-26-2007, 10:41 PM
    Answer
    • Loading...
    • neutrino
    • Joined on 09-21-2006, 2:21 AM
    • Posts 227
    • Points 759

    Hi,

    I am using the new SetFocus method of the ScriptManager class.

    Example:

    ScriptManager1.SetFocus(TextBox2);

    I works with the last release of Microsoft AJAX.

    Hope this helps,

    David
     

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    05-27-2007, 3:21 AM
    • Loading...
    • mepurathu
    • Joined on 05-16-2006, 8:31 PM
    • Posts 7
    • Points 25

    Wow; that works great; Thanks for the quick reply.

     Jacob

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-01-2007, 10:49 AM
    • Loading...
    • venkatzeus
    • Joined on 10-27-2006, 2:27 PM
    • Posts 483
    • Points 1,304

    I am using the Atlas beta version. I am having the same textbox focus problem. I tried to use the setFocus(), but i couldnt find the setFocus() of the scriptmanager.

    Where to use

    ScriptManager1.SetFocus(TextBox2);

    I just have the

    ScriptManager1.Focus() method. please help.

    Thanks in Advance

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-02-2007, 12:33 AM

    Use the latest version of ASP.NET AJAX

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-03-2007, 10:18 PM
    • Loading...
    • greatranil
    • Joined on 05-22-2007, 12:54 PM
    • Posts 1
    • Points 2

    Thanks lot

    I also having same prob and search the web for solution. This is tha only place that I found a resolution.

    Thanks lot again

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    02-21-2008, 7:23 AM
    • Loading...
    • noldor
    • Joined on 04-26-2007, 7:28 PM
    • Posts 12
    • Points 11

    When using masterPage system, i can't access the "ScriptManager1" object within the codebehind of ContentPage file..

    Is there any solution for this ??? 

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    02-21-2008, 11:39 AM
    • Loading...
    • joel.tulloch
    • Joined on 07-19-2007, 6:50 PM
    • Waterloo, Canada
    • Posts 47
    • Points 292

    You access the scriptManager from a content page using the static GetCurrent method:

     

    ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
      
  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    02-26-2008, 9:39 AM
    • Loading...
    • davearia
    • Joined on 08-01-2007, 3:02 PM
    • Posts 7
    • Points 6

    Good post,

    But I found another issue. The text box I am using is wired up to a  AutoCompleteExtender control. This works fine but as soon as I set focus using the above code this autocomplete does not work anymore.

     Any ideas?

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    03-06-2008, 2:15 PM
    • Loading...
    • rumax96
    • Joined on 10-01-2007, 5:37 PM
    • Posts 89
    • Points 43

    Thanks to all contributor,  Its been three days i have been googling. Finally

    Feels good, for a newbie in AJAX

     

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-10-2008, 2:57 AM
    • Loading...
    • Ahmish
    • Joined on 04-02-2007, 8:10 PM
    • Pakistan
    • Posts 207
    • Points 737

    I am using GridView and there is a textbox in that on which i need my focus but this code is not working in my case. where i have to write the ScriptManager1.setfocus() code

    please tell me ASAP 

    "Hope its your Solution so Mark it as Answer"

    Ahmad Sheikh
    Microsoft Valuable Geek :: Freelancer & Consultant
    http://sharpcontents.blogspot.com
  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-10-2008, 8:55 AM
    • Loading...
    • rumax96
    • Joined on 10-01-2007, 5:37 PM
    • Posts 89
    • Points 43

     It doesnt matter where you write it will place at the end of the page making it to execute only when the whole page is loaded, if you do view source on your page.

    Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)

    'Find the textbox in the grid,  I have done for the footer row but you can do row index to get the right row and the control

    Dim UserIDTexbox As TextBox = CType(Me.uxGridView.FooterRow.FindControl("UserIDTextBox"), TextBox) 

    sm.SetFocus(UserIDTexbox )

     I am sure it works, If not then paste your code.

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-19-2008, 1:02 PM
    • Loading...
    • augustuslesh
    • Joined on 06-19-2008, 3:33 PM
    • Posts 4
    • Points 8

     

    Hi All,

    I need one help from you. I am trying to set focus to different controls after my different PostBack events, but this.ScriptManager1.SetFocus(myControlName.ClientID); is not working.

    In my ASPX page I have RAD CONTROLS (RadComboBox and RadCalendar) and also I am using AjaxControlToolkit Controls (ToolkitScriptManager and ModalPopupExtender) also.

    In my page, RadComboBox is in UpdatePanel - ContentTemplate area and on its SelectedIndexChanged function I am trying to set focus to another Control using the above code:

    this.ScriptManager1.SetFocus(myControlName.ClientID);

    but this is not working... Do anyone know any solution for this?

    Thanks...
  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    06-20-2008, 10:17 AM
    • Loading...
    • augustuslesh
    • Joined on 06-19-2008, 3:33 PM
    • Posts 4
    • Points 8


    Hi All,

    I got solution for the above issue. I was trying to set focus using the code 

    this.ScriptManager1.SetFocus(myControlName.ClientID);

    and I wrote this code in my SelectedIndexChanged Event Function. That time this code didn't work.

     Now I changed this code to Page Load Function i.e.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                     this.ScriptManager1.SetFocus(myControlName.ClientID);
            }
            else
            {
                    this.ScriptManager1.SetFocus(GetPostBackControl(this.Page).ClientID);
            }
        }

    Here I am using a function GetPostBackControl() to identify the current postbacking control and I am setting focus to the same control. If you want to move focus to any other control, just use a Switch Statment.


        public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page)
        {
            Control control = null;
            string ctrlname = page.Request.Params["__EVENTTARGET"];
            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = page.FindControl(ctrlname);
            }
            // if __EVENTTARGET is null, the control is a button type and we need to
            // iterate over the form collection to find it
            else
            {
                string ctrlStr = String.Empty;
                Control c = null;
                foreach (string ctl in page.Request.Form)
                {
                    // handle ImageButton controls ...
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        ctrlStr = ctl.Substring(0, ctl.Length - 2);
                        c = page.FindControl(ctrlStr);
                    }
                    else
                    {
                        c = page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.Button ||
                             c is System.Web.UI.WebControls.ImageButton)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control;
        }

    Thank you,
     

  • Re: ASP.Net AJAX - Cannot Set focus to a TextBox inside an Update Panel

    10-08-2008, 3:41 PM
    • Loading...
    • evaleah
    • Joined on 06-19-2006, 7:36 PM
    • Posts 42
    • Points 38

    Wow, what a great thread.  I found my answer instantly.  Thanks to everyone.

Page 1 of 2 (18 items) 1 2 Next >