How to make the Default focus in content page from master page

后端 未结 6 1846
我在风中等你
我在风中等你 2021-01-07 07:57

I have masterpage with content place holder. i have contentpage which is using master page . in all my content page i need to default focus on the text b

相关标签:
6条回答
  • 2021-01-07 08:00

    If you use jQuery, a possible solution is:

    1. Give the textbox you want to set focus to a special class. "focus" works well for this purpose.

    2. Write code such as the following in your master page or included by your master page in a js script file:

      $(document).ready    
      (
      
        function()
        {
          //get an array of DOM elements matching the input.focus selector
          var focusElements = $("input.focus").get();
      
          //if a focus element exists
          if(focusElements.length > 0)
          {
            focusElements[0].focus();
          }
        }
      );
      

    A similar approach using vanilla JavaScript would be to tag the textbox with a special attribute. Let's use focus.

    window.onload = function()
    {
      //get all input elements
      var inputElements = document.getElementsByTagName("input");
    
      var elementToFocus = null;
    
      for(var i = 0; i < inputElements.length; ++i) 
      {
        var focusAttribute = inputElements[i].getAttribute("focus");
    
        if(focusAttribute)
        {
          elementToFocus = inputElements[i];
          break;
        }
      }
    
      if(elementToFocus)
      {
        elementToFocus.focus();
      }
    };
    
    0 讨论(0)
  • 2021-01-07 08:02
    <script language="javascript" type="text/javascript" >
    
    
    window.onload=function(){
    var t= document.getElementById('<%=TextBox1.clientID %>');
    t.focus();
    }
    
    
    </script>
    
    0 讨论(0)
  • 2021-01-07 08:04

    Indiscriminate JavaScript approach to selecting the first valid input field on a page:

    function SelectFirstInput() {
        var bFound = false;
        for (f = 0; f < document.forms.length; f++) {
            // for each element in each form
            for (i = 0; i < document.forms[f].length; i++) {
                // if it's not a hidden element
                if (document.forms[f][i].type != "hidden") {
                    // and it's not disabled
                    if (document.forms[f][i].disabled != true) {
                        // set the focus to it
                        document.forms[f][i].focus();
                        var bFound = true;
                    }
                }
                // if found in this element, stop looking
                if (bFound == true)
                    break;
            }
            // if found in this form, stop looking
            if (bFound == true)
                break;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 08:05

    try using this...

    ((TextBox)Master.FindControl("txtRequiredFocus")).Focus();
    
    0 讨论(0)
  • 2021-01-07 08:18

    You could include this in your master page's load event:

    // if the ID is constant you can use this:
    /*TextBox textBox = (TextBox)Page.Controls[0]
                                    .FindControl("ContentPlaceHolder1")
                                    .FindControl("myTextBox");
    */
    
    // this will look for the 1st textbox without hardcoding the ID
    TextBox textBox = (TextBox)Page.Controls[0]
                                .FindControl("ContentPlaceHolder1")
                                .Controls.OfType<TextBox>()
                                .FirstOrDefault();
    
    if (textBox != null)
    {
        textBox.Focus();
    }
    

    This would match up with a content page that has the following markup:

    <asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:TextBox ID="myTextBox" runat="server" />
    </asp:Content>
    

    EDIT: if LINQ isn't an option then you can use this instead:

    foreach (Control control in Page.Controls[0].FindControl("ContentPlaceHolder1").Controls)
    {
        if (control is TextBox)
        {
            ((TextBox)control).Focus();
            break;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 08:25
    Control masterC = 
    Page.Master.FindControl("ContentPlaceHolder1");
        TextBox TextBox1 = 
    masterC.FindControl("TextBoxUsername") as TextBox;
    
    TextBox1.Focus();
    
    0 讨论(0)
提交回复
热议问题