admin 管理员组

文章数量: 1086019

I am using ASP.NET 2.0 and VB.NET (C# code will also help me out)

On top of my page I have a button called btnViewRecords. When the User click on the button I want to set focus to another button or label further down on the same page. How can this be done.

This code does not work for me..............

btnTheRecords.Focus()

or

lblHeader.Focus()

Thanks in advance!!

Edit: Even if my code did work, i dont want to reload the page every time.

I am using ASP.NET 2.0 and VB.NET (C# code will also help me out)

On top of my page I have a button called btnViewRecords. When the User click on the button I want to set focus to another button or label further down on the same page. How can this be done.

This code does not work for me..............

btnTheRecords.Focus()

or

lblHeader.Focus()

Thanks in advance!!

Edit: Even if my code did work, i dont want to reload the page every time.

Share Improve this question edited Apr 14, 2015 at 16:04 Juergen 12.7k7 gold badges41 silver badges56 bronze badges asked Sep 24, 2009 at 10:17 EtienneEtienne 7,20143 gold badges110 silver badges163 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 2

Here's a relatively simple solution...

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

You can use JavaScript to do this.

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

By setting focus what do you mean. Do you want to bring this control into view if it is way down the page. Then there are better ways to do this.

Edit:

You can place an anchor tag near the button or the bale and set

location.href = "#anchorId";

where anchorId is the id of the anchor tag.

will move the focus to the anchor tag.

Try this,

I wrote a small code and it seems to be working for me. sorry this is in C# but if you click on a button and set focus on another button then page will automatically scroll down for you.

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/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3/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 Code Behind

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

I'm not sure exactly of the problem you're having. But in cases where the SetFocus() function and such just don't work (and they often don't in VB for some reason). I've often used workarounds such as shortcut Keys & mnemonics in bination with SendKeys() to get focus where I need it to be.

Depending on whether the buttons you are talking about are in your ASP.NET code or in your VB.NET code, this may be a viable workaround for you as well.

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

Reference: .scrollIntoView

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

本文标签: cSetting focus onto another contol from button clickStack Overflow