locked
TabContainer/TabPanel set focus to control on load and in postback RRS feed

  • Question

  • User-548439260 posted

    I am using the Tabcontainer/Tab Panel for my ASP.NET application.  On page load, I am setting the ActiveTabIndex to the first tab:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    LoadFormGrid();
                    TabContainer1.ActiveTabIndex = 0;
                }
            }

    I would when the tab displays, I would like to set the focus to a textbox control on the tab panel.

    How can I do this?  (I have tried various things posted on the web to no avail)

    I also have an event (drop down list selected index changed) in which I would like to set the focus to a specific control on the tab panel.  How can I do this?

    Thank you,

    Becky McDermott

    Thursday, September 26, 2013 12:34 PM

Answers

  • User839260933 posted
    protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
                DropDownList ddl2 = (DropDownList)gr.FindControl("ddl2");
                ddl2.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 27, 2013 1:47 AM
  • User555306248 posted

    Hi,

    You can call the following two line in javascript

     $find('tabcontainer1BehaviorID').set_activeTabIndex(2);

    $get('textoxclientID').focus();

    http://forums.asp.net/t/1407804.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 29, 2013 11:29 PM

All replies

  • User839260933 posted

    Hi

    Please try this code i dropdown selectionchanged event

    txt = (TextBox)TabContainer1.Parent.FindControl("txtName");
    txt.Focus(); 



    Thursday, September 26, 2013 12:53 PM
  • User-548439260 posted

    I had to modify your code a bit:

    tb = (TextBox)TabContainer1.Tabs[0].FindControl("TextBoxCustomerName");
    tb.Focus();

    The code runs fine but focus is NOT set.  I do have an asp:RequiredFieldValidator set on the textbox as well.  I'm not sure if that is preventing focus from being set.

    I have spent hours trying to figure out how to set the focus.  It seems related to the AJAX control toolkig TabContainer.

    Thursday, September 26, 2013 1:05 PM
  • User839260933 posted
    function SetFocus() {
    var txtName = $get("TextBoxCustomerName.ClientId"); 
    txtName.focus();
    }
    function pageLoad() {
    SetFocus();
    }

    Thursday, September 26, 2013 1:22 PM
  • User-548439260 posted

    That worked great with some jquery syntax modification:

    <script type="text/javascript">
        function SetFocus() {
            
    var txtName = $("#<%=TextBoxCustomerName.ClientID%>");
            
    txtName.focus();
              }

           function pageLoad() {
           SetFocus();
           }

          function SetFocusStandard() {
          
    var ddlStandard = $("#<%=DropDownListStandard.ClientID%>");
           
    ddlStandard.focus();
          } 
    </script>

     

    On page load, focus is now set to my textbox. 

     

    On my dropdownlist SelectedIndexChanged event, I am trying to call SetFocusStandard() but it is not working.  Here is the line of code from my code behind:

    Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "SetFocusStandard();", true);

     

    I don't get any errors but the focus is not changed to my drop down list.  Any ideas?

     

    Thank you!

    Thursday, September 26, 2013 1:49 PM
  • User839260933 posted

    Try this 

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>SetFocusStandard();</script>", false);



    Thursday, September 26, 2013 1:54 PM
  • User-548439260 posted

    I tried that but it still isn't setting the focus to the other drop-down-list.

    Thursday, September 26, 2013 2:01 PM
  • User-548439260 posted

    I put an alert insde the "SetFocus()" function that is called on pageLoad().  I think the drop down is not focusing because on postBack, the pageLoad() JS function is re-setting the focus to the text box. 

    Maybe I can call the "SetFocus()" command from my server side Page Load (only when it is not postback).  Does that sound like a good idea?

    Thursday, September 26, 2013 2:43 PM
  • User-548439260 posted

    That did not work.  I removed

    function pageLoad()
    {
         SetFocus();
    }

    and added

    protected

    void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
       
    {
                LoadFormGrid();
                TabContainer1.ActiveTabIndex = 0;

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>SetFocus();</script>", false);
       
    }
    }

    but, the focus does NOT set to the text box.  If I put function pageLoad()  back in, focus sets to the text box but I cannot set focus to another control in the drop down list selectedIndexChanged event.

    Thursday, September 26, 2013 3:02 PM
  • User839260933 posted
    protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
                DropDownList ddl2 = (DropDownList)gr.FindControl("ddl2");
                ddl2.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 27, 2013 1:47 AM
  • User555306248 posted

    Hi,

    You can call the following two line in javascript

     $find('tabcontainer1BehaviorID').set_activeTabIndex(2);

    $get('textoxclientID').focus();

    http://forums.asp.net/t/1407804.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 29, 2013 11:29 PM