【问题标题】:Setting focus onto another contol from button click通过单击按钮将焦点设置到另一个控件
【发布时间】:2010-12-01 01:06:09
【问题描述】:

我正在使用 ASP.NET 2.0 和 VB.NET(C# 代码也会帮助我)

在我的页面顶部,我有一个名为 btnViewRecords 的按钮。当用户单击按钮时,我想将焦点设置到同一页面上的另一个按钮或标签。如何才能做到这一点。

此代码不适用于我......

btnTheRecords.Focus()

或

lblHeader.Focus()

提前致谢!!

编辑: 即使我的代码确实有效,我也不想每次都重新加载页面。

【问题讨论】:

    标签: c# asp.net javascript vb.net


    【解决方案1】:

    这里有一个比较简单的解决方案...

        <asp:Button ID="Button1" runat="server" Text="Button" 
            OnClientClick="return SetFocus()" />
    
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    
        <script type="text/javascript">
            function SetFocus() {
                document.getElementById('<%=TextBox2.ClientID %>').focus();
                //or this if you're using jQuery
                //$("#<%=TextBox2.ClientID %>").focus();
                return false;
            }
        </script>
    

    【讨论】:

      【解决方案2】:

      您可以使用 JavaScript 来执行此操作。

      document.getElementById ( "btnTheRecords" ).focus();
      

      设置焦点是什么意思。如果该控件位于页面下方,是否要将其显示在视图中。然后有更好的方法来做到这一点。

      编辑:

      您可以在按钮或草捆附近放置一个锚标记并设置

      location.href = "#anchorId";
      

      其中anchorId是锚标签的id。

      将焦点移动到锚标记。

      【讨论】:

      • 我只想将整个页面向下移动,以便用户无需自己向下滚动即可查看记录。
      • 通过#tag 使用页内超链接是实现此目的的最简单方法。您需要做的就是将按钮变成超链接
      【解决方案3】:

      试试这个,

      我写了一个小代码,它似乎对我有用。抱歉,这是在 C# 中,但如果您单击一个按钮并将焦点设置在另一个按钮上,那么页面将自动为您向下滚动。

      ASP.NET

      <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title></title>
      </head>
      <body>
          <form id="form1" runat="server">
          <div> 
              <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    
          </div>
          <div style="height:50px;"></div>
          <div>
              <asp:Button ID="Button1" runat="server" Text="Button 1" onclick="Button1_Click" />
          </div>        
          <div style="height:1000px;"></div>
          <div>    
              <asp:Button ID="Button2" runat="server" Text="Button 2" onclick="Button2_Click" />    
          </div>        
          </form>
      </body>
      </html>
      

      AND 后面的代码

      using System;
      
      public partial class _Default : System.Web.UI.Page 
      {
          protected void Page_Load(object sender, EventArgs e)
          {
      
          }
          protected void Button1_Click(object sender, EventArgs e)
          {
              Button2.Focus();
              Label1.Text = "button1 clicked & button2 in focus";
          }
          protected void Button2_Click(object sender, EventArgs e)
          {
              Button1.Focus();
              Label1.Text = "button2 clicked & button1 in Focus";
          }
      }
      

      【讨论】:

        【解决方案4】:

        我不确定您遇到的具体问题。但是在 SetFocus() 函数等不起作用的情况下(并且由于某种原因它们通常在 VB 中不起作用)。我经常将快捷键和助记符与 SendKeys() 结合使用等变通方法来获得需要的焦点。

        根据您所谈论的按钮是在您的 ASP.NET 代码中还是在您的 VB.NET 代码中,这对您来说可能也是一个可行的解决方法。

        【讨论】:

          【解决方案5】:
          document.getElementById("spanId").scrollIntoView(false)
          

          参考: .scrollIntoView

          【讨论】:

            【解决方案6】:
            如果按钮是服务器端,设置“OnClientClick” 对它的 javascript 函数执行操作。取消窗口 事件,因此页面位置不会改变。 函数转到(){ window.event.cancelBubble = true; window.event.returnValue = 假; document.getElementById("pageloction").focus() } asp:Button runat="server" ID="btnTheRecords" OnClientClick="javascript:goto()" /> 跨度 id="pageloction">

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-11-25
              • 2014-11-22
              • 2016-10-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多