Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting Control Focus

Posted on 2005-02-26
5
Medium Priority
?
182 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
  • 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

564 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