?
Solved

Setting Control Focus

Posted on 2005-02-26
5
Medium Priority
?
179 Views
Last Modified: 2010-04-16
I'm trying to set focus on a textbox when I come into a web form (C#).  How do I do this?
0
Comment
Question by:Remulac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:seeflat
ID: 13413239
I use javascript to do this...
You could put the javascript on the .aspx page in the HEAD section:

    <script language="javascript">
    <!--
    function SetFocus()
    {
        document.home.txtUsername.focus();
    }
    -->
    </script>
</head>
<body onload="SetFocus()">
...
<form id="home"...
<input name="txtUsername" type="text" ...

, or if you want to do it in code-behind:
private void SetFocus()
{
    string jScript = "<script language=\"javascript\">" + Environment.NewLine;
    jScript += "document.home.txtUsername.focus();" + Environment.NewLine;
    jScript += "</script>" + Environment.NewLine;

    this.RegisterStartupScript("focusScript", jScript);
}

call that function in the page load event handler:
private void Page_Load(object sender, System.EventArgs e)
{
    SetFocus();
}


hth,
seeflat

0
 

Author Comment

by:Remulac
ID: 13415563
seeflat,

For whatever reason, this doesn't seem to work.  It looks like it should be pretty straightforward.  Any ideas as to what I could be doing wrong?  Here's where I call the fuction:

                        if(!IsPostBack)
                        {
                              // Populate the Project Officers Dropdown listbox
                              PageControls.PopulateDropDownList(SortByProjectOfficer,"usp_GroupContactsSelectAll","NAME","RESOURCE_ID","GroupName","CATS Project Officer Group");
                              ErrorLabel.Visible=false;
                              SetSearchFocus();

                        }


And here's the function itself:


            private void SetSearchFocus()
            {
                  string jScript = "<script language=\"javascript\">" + Environment.NewLine;
                  jScript += "document.home.Search.focus();" + Environment.NewLine;
                  jScript += "</script>" + Environment.NewLine;

                  this.RegisterStartupScript("focusScript", jScript);
            }
0
 
LVL 5

Expert Comment

by:seeflat
ID: 13417640
Have you named the form on the page?

<form name="home" id="home">

Also, be sure your control (Search) is getting the right name as well.

seeflat
0
 
LVL 9

Accepted Solution

by:
sun4sunday earned 1000 total points
ID: 13418257

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21325813.html
The accepted answer is
set the focus in the Form_Activated event. The load event is fired first but the controls are not initialized and therefore cannot get the focus.

sun4sunday
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 13418456
To set the focus of an input field we need to use client-side script. Specifically, we call the input field's focus() method. To have this occur when the page is loaded, we can wire up the BODY tag's onload event to some client-side event handler that contains code that calls the input field's focus() method. For example, the following simple HTML page demonstrates how to accomplish this:

<html>
<head>
  <script language="JavaScript">
    function setFocus()
    {
      frmInfo.name.focus();
    }
  </script>
</head>
<body onload="setFocus();">
  <form id="frmInfo">
    Name: <input type="text" id="name" /><br />
    Age:  <input type="text" id="age" />
  </form>
</body>
</html>


For some more examples of this concept, there are a ton of online resources worth checking out, such as Focus OnLoad. A Google search can yield more results...

taken from
http://aspnet.4guysfromrolla.com/articles/090902-1.aspx

sun4sunday
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question