views:

337

answers:

7

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

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

+1  A: 

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.

rahul
I just want to move the whole page down so that it will take the User to where he can view the records without scrolling down by himself.
Etienne
Using the in-page hyperlink using #tag is the simplest way of achieving this. All you need to do is make the button into a hyperlink
Sanket
A: 
document.getElementById("spanId").scrollIntoView(false)

Reference: .scrollIntoView

rick schott
A: 

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

Jrud
A: 
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">

Gareth
A: 

Isn't ".focus()" case-sensitive. Not too sure in ASP2.0, but I think it should be:

btnTheRecords.focus();
aneuway
Using Server side code will cause the page to refresh
Sanket
A: 

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

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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";
    }
}
Ved
+1  A: 

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 SetOtherFocus() {
            document.getElementById('<%=TextBox2.ClientID %>').focus();
            //or this if you're using jQuery
            //$("#<%=TextBox2.ClientID %>").focus();
            return false;
        }
    </script>
ianpoley