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

风流意气都作罢 提交于 2019-12-01 02:15:24

try using this...

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

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

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


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


</script>
David Andres

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!