C# 通过单击按钮将焦点设置到另一个控件上

C# 通过单击按钮将焦点设置到另一个控件上,c#,asp.net,javascript,vb.net,C#,Asp.net,Javascript,Vb.net,我正在使用ASP.NET2.0和VB.NET(C代码也会帮助我) 在我的页面顶部有一个名为btnViewRecords的按钮。当用户单击按钮时,我想将焦点设置为同一页面上的另一个按钮或标签。如何做到这一点 此代码不适用于我 btnTheRecords.Focus() 或 提前谢谢 编辑: 即使我的代码可以工作,我也不想每次都重新加载页面。您可以使用JavaScript来完成这项工作 document.getElementById ( "btnTheRecords" ).focus(); 设置

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

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

此代码不适用于我

btnTheRecords.Focus()

提前谢谢

编辑:
即使我的代码可以工作,我也不想每次都重新加载页面。

您可以使用JavaScript来完成这项工作

document.getElementById ( "btnTheRecords" ).focus();
设置焦点是什么意思。如果此控件位于页面下方,是否要将其显示在视图中。还有更好的方法可以做到这一点

document.getElementById ( "btnTheRecords" ).focus();
编辑:

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

location.href = "#anchorId";
其中anchorId是锚定标记的id

document.getElementById("spanId").scrollIntoView(false)

将焦点移动到锚标记。

您可以使用JavaScript来完成此操作

document.getElementById ( "btnTheRecords" ).focus();
document.getElementById("spanId").scrollIntoView(false)
设置焦点是什么意思。如果此控件位于页面下方,是否要将其显示在视图中。还有更好的方法可以做到这一点

document.getElementById ( "btnTheRecords" ).focus();
编辑:

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

location.href = "#anchorId";
其中anchorId是锚定标记的id

document.getElementById("spanId").scrollIntoView(false)
将焦点移动到锚定标记

document.getElementById("spanId").scrollIntoView(false)
参考:

参考:

我不确定你到底有什么问题。但是在SetFocus()函数等不起作用的情况下(由于某些原因,它们在VB中通常不起作用)。我经常将快捷键和助记符等变通方法与SendKeys()结合使用,以获得所需的焦点


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

我不确定您到底遇到了什么问题。但是在SetFocus()函数等不起作用的情况下(由于某些原因,它们在VB中通常不起作用)。我经常将快捷键和助记符等变通方法与SendKeys()结合使用,以获得所需的焦点

If the button is server side, set the "OnClientClick" action on it to a javascript function. Cancel windows events so the page location does not change. function goto(){ window.event.cancelBubble = true; window.event.returnValue = false; document.getElementById("pageloction").focus() } asp:Button runat="server" ID="btnTheRecords" OnClientClick="javascript:goto()" /> span id="pageloction"> 根据您正在谈论的按钮是在ASP.NET代码中还是在VB.NET代码中,这可能也是一种可行的解决方法

If the button is server side, set the "OnClientClick" action on it to a javascript function. Cancel windows events so the page location does not change. function goto(){ window.event.cancelBubble = true; window.event.returnValue = false; document.getElementById("pageloction").focus() } asp:Button runat="server" ID="btnTheRecords" OnClientClick="javascript:goto()" /> span id="pageloction"> 如果按钮是服务器端的,则设置“OnClientClick” 将其上的操作添加到javascript函数。取消窗口 事件,以便页面位置不会更改。 函数goto(){ window.event.cancelBubble=true; window.event.returnValue=false; document.getElementById(“PageLocation”).focus() } asp:Button runat=“server”ID=“btnTheRecords”OnClientClick=“javascript:goto()”/> span id=“PageLocation”> 如果按钮是服务器端的,则设置“OnClientClick” 将其上的操作添加到javascript函数。取消窗口 事件,以便页面位置不会更改。 函数goto(){ window.event.cancelBubble=true; window.event.returnValue=false; document.getElementById(“PageLocation”).focus() } asp:Button runat=“server”ID=“btnTheRecords”OnClientClick=“javascript:goto()”/> span id=“PageLocation”> 试试这个

我写了一个小代码,它似乎对我有用。抱歉,这是在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>
试试这个

我写了一个小代码,它似乎对我有用。抱歉,这是在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>

这里有一个相对简单的解决方案

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

函数SetFocus(){
document.getElementById(“”).focus();
//或者,如果您使用jQuery
//$(“#”)焦点();
返回false;
}

这里有一个相对简单的解决方案

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

函数SetFocus(){
document.getElementById(“”).focus();
//或者,如果您使用jQuery
//$(“#”)焦点();
返回false;
}

我只想向下移动整个页面,这样用户就可以在不向下滚动的情况下查看记录。使用#标记的页面内超链接是实现这一点的最简单方法。你所需要做的就是将按钮变成超链接。我只想将整个页面向下移动,这样用户就可以在不向下滚动的情况下查看记录。使用#标记的页面内超链接是实现这一点的最简单方法。你所需要做的就是把按钮变成一个超链接