admin管理员组

文章数量:1289483

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 box so that the user can directly type in text box instead moving the mouse over the textbox. in some page there is no text box so that i donnot nnet keep default focus over there

Is there any way i can do it in my master page once and can reuse that in all my content page

thank you

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 box so that the user can directly type in text box instead moving the mouse over the textbox. in some page there is no text box so that i donnot nnet keep default focus over there

Is there any way i can do it in my master page once and can reuse that in all my content page

thank you

Share Improve this question edited Jun 21, 2011 at 15:59 Muhammad Akhtar 52.2k37 gold badges139 silver badges191 bronze badges asked Sep 10, 2009 at 5:23 happysmilehappysmile 7,77738 gold badges106 silver badges182 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 3

try using this...

((TextBox)Master.FindControl("txtRequiredFocus")).Focus();

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;
    }
}

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;
    }
}
<script language="javascript" type="text/javascript" >


window.onload=function(){
var t= document.getElementById('<%=TextBox1.clientID %>');
t.focus();
}


</script>

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();
  }
};
Control masterC = 
Page.Master.FindControl("ContentPlaceHolder1");
    TextBox TextBox1 = 
masterC.FindControl("TextBoxUsername") as TextBox;

TextBox1.Focus();

本文标签: cHowto maketheDefault focus incontent page from master page Stack Overflow