如何使母版页中内容页的默认焦点

发布于 2024-08-04 12:26:35 字数 169 浏览 3 评论 0原文

我有带有内容占位符的母版页。我有使用母版页的内容页。在我的所有内容页面中,我需要默认关注文本框,以便用户可以直接在文本框中键入内容,而不是将鼠标移到文本框上。在某些页面中没有文本框,因此我不会将默认焦点保留在那里

有什么方法可以在我的母版页中执行一次并可以在我的所有内容页面中重复使用它

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅忆 2024-08-11 12:26:36

您可以将其包含在母版页的加载事件中:

// 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();
}

这将与具有以下标记的内容页匹配:

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

编辑:如果 LINQ 不是一个选项,那么您可以使用它:

foreach (Control control in Page.Controls[0].FindControl("ContentPlaceHolder1").Controls)
{
    if (control is TextBox)
    {
        ((TextBox)control).Focus();
        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;
    }
}
云朵有点甜 2024-08-11 12:26:36

不加区别地选择页面上第一个有效输入字段的 JavaScript 方法:

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

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;
    }
}
原来是傀儡 2024-08-11 12:26:36
<script language="javascript" type="text/javascript" >


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


</script>
<script language="javascript" type="text/javascript" >


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


</script>
老娘不死你永远是小三 2024-08-11 12:26:36

如果您使用 jQuery,一个可能的解决方案是:

  1. 将您想要将焦点设置为特殊类的文本框。 “focus”非常适合此目的。

  2. 在母版页中编写如下代码或将母版页包含在 js 脚本文件中:

    $(文档).ready    
    (
    
      功能()
      {
        //获取与input.focus选择器匹配的DOM元素数组
        var focusElements = $("input.focus").get();
    
        //如果焦点元素存在
        if(focusElements.length > 0)
        {
          focusElements[0].focus();
        }
      }
    );
    

使用普通 JavaScript 的类似方法是使用特殊属性标记文本框。让我们使用焦点。

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

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();
  }
};
疯狂的代价 2024-08-11 12:26:36
Control masterC = 
Page.Master.FindControl("ContentPlaceHolder1");
    TextBox TextBox1 = 
masterC.FindControl("TextBoxUsername") as TextBox;

TextBox1.Focus();
Control masterC = 
Page.Master.FindControl("ContentPlaceHolder1");
    TextBox TextBox1 = 
masterC.FindControl("TextBoxUsername") as TextBox;

TextBox1.Focus();
装迷糊 2024-08-11 12:26:35

尝试使用这个...

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

try using this...

((TextBox)Master.FindControl("txtRequiredFocus")).Focus();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文