How do I set focus to a control?

How do I set focus to a control?  I'm getting this error:

A form tag with runat=server must exist on the Page to use SetFocus() or the Focus property.
LVL 1
HLRosenbergerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajapandian_81Commented:
Add runat="server" in form tag like below.

<form id="form1" runat="server" > 
0
HLRosenbergerAuthor Commented:
It already has that.  As do all the controls on the form.
0
intlaqaCommented:
Hi,

Put your controls within a form which runs at the for example:

<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </div>
    </form>
</body>

Then call the Focus method, for example:

Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Focus();
    }
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

rajapandian_81Commented:
Can you post some part of code?
0
HLRosenbergerAuthor Commented:
Here's the whole form:


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>

<!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>Maintain USLIG Logos</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 546px; width: 803px;">
        <asp:Label ID="Label1" runat="server" Height="40px"
            Text="USLIG Cost Center Maintenance" Width="384px" Font-Bold="True"
            Font-Size="Larger"></asp:Label>
        <br />
        <asp:Panel ID="Panel2" runat="server" BackColor="#5D7B9D" Font-Bold="True"
            ForeColor="White" Height="42px" Width="773px">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cost Center
            Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Used&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Remaining<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Budget&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Budget&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Budget<br />
        </asp:Panel>
        <asp:Panel ID="Panel1" runat="server" Height="359px" ScrollBars="Vertical"
            Width="768px" BorderStyle="Double" BorderWidth="2px">
            &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:iWayDBSqlConnectionString %>"
                SelectCommand="SELECT 0 as selected, CostCenters.Name, CostCenters.OrderQuota, CostCentersProperties.UsedBudget, 0 as [Remaining Budget], CostCenters.CostCenterID

FROM CostCenters

INNER JOIN CostCentersProperties ON CostCenters.CostCenterID = CostCentersProperties.CostCenterID WHERE (CostCenters.CustomerID = 1310) ORDER BY CostCenters.Name">
            </asp:SqlDataSource>
            <asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False"
                BorderWidth="0px" CellPadding="4" ForeColor="#333333" GridLines="Vertical"
                Height="141px" ShowHeader="False" Width="750px">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
        </asp:Panel>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <br />
&nbsp;<asp:Panel ID="Panel3" runat="server" BackColor="#5D7B9D" BorderStyle="Groove"
            Height="63px" Width="486px">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="White"
                Text="New Total Budget: "></asp:Label>
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtNewAmount" runat="server" Width="95px"></asp:TextBox>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="btnUpdate" runat="server" Text="Update Cost Centers"
                Width="154px" />
        </asp:Panel>
    </div>
    </form>
</body>
</html>
0
intlaqaCommented:
Here is the complete page, Page_Load sets the focus to TextBox2, and when you click on the button, it sets the focus to TextBox1:


<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Focus();
    }

    protected void btnClicked(object sender, EventArgs e)
    {
        TextBox1.Focus();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"></head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button OnClick="btnClicked" ID="Button1" runat="server" Text="Button" />
    </form>
</body>
</html>

Open in new window

0
rajapandian_81Commented:
HLRosenberger:

In which event, which control you are trying to focus?
0
HLRosenbergerAuthor Commented:
The text box, <asp:TextBox ID="txtNewAmount"
0
intlaqaCommented:
rajapandian asked also for the 'event' where you are calling the Focus method.

Also, do you Add/Remove some controls dynamically? Do you call Page.Controls.Remove or Page.Controls.Add ? perhaps you move the panel or something similar on the runtime somewhere outside the Form?
0
HLRosenbergerAuthor Commented:
Nope.  none of that.  I have not touched the ASP code.  The code was generated visually by the IDE.
0
rajapandian_81Commented:
HLRosenberger:

When you want to focus the control? In page load event or somewhere?
Can you post your code for focus the control?
0
HLRosenbergerAuthor Commented:
I did post the code.  It's all there.  Page Load or form Init.    How is Page Load related to Form Init.  
0
rajapandian_81Commented:
In your code change the body tag from
<body>

to
<body onload="document.getElementById('txtNewAmount').focus();">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HLRosenbergerAuthor Commented:
That did it!   Thanks!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.