[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Setting focus to control

Posted on 2005-04-18
4
Medium Priority
?
270 Views
Last Modified: 2010-04-16
I am new to web forms (and C# for that matter)..I am trying to use this code I found on the web courtesy of Ryan Farley, that sets the focuse to a sepcified control.  I am getting the following error:
The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)

Help?


public static void SetFocus(Control control)
            {
                  StringBuilder sb = new StringBuilder();
 
                  sb.Append("\r\n<script language='JavaScript'>\r\n");
                  sb.Append("<!--\r\n");
                  sb.Append("function SetFocus()\r\n");
                  sb.Append("{\r\n");
                  sb.Append("\tdocument.");
 
                  Control p = control.Parent;
                  while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
 
                  sb.Append(p.ClientID);
                  sb.Append("['");
                  sb.Append(control.UniqueID);
                  sb.Append("'].focus();\r\n");
                  sb.Append("}\r\n");
                  sb.Append("window.onload = SetFocus;\r\n");
                  sb.Append("// -->\r\n");
                  sb.Append("</script>");
 
                  control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
            }
0
Comment
Question by:PsychoDazey
  • 3
4 Comments
 
LVL 8

Expert Comment

by:vinhnl
ID: 13806978
You must "using System.Text;"

Good luck
VINHNL
0
 
LVL 8

Accepted Solution

by:
vinhnl earned 500 total points
ID: 13807009
Use this code:

public static void SetFocus(Control control)
          {
               System.Text.StringBuilder sb = new StringBuilder();
 
               sb.Append("\r\n<script language='JavaScript'>\r\n");
               sb.Append("<!--\r\n");
               sb.Append("function SetFocus()\r\n");
               sb.Append("{\r\n");
               sb.Append("\tdocument.");
 
               Control p = control.Parent;
               while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
 
               sb.Append(p.ClientID);
               sb.Append("['");
               sb.Append(control.UniqueID);
               sb.Append("'].focus();\r\n");
               sb.Append("}\r\n");
               sb.Append("window.onload = SetFocus;\r\n");
               sb.Append("// -->\r\n");
               sb.Append("</script>");
 
               control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
          }
0
 
LVL 8

Expert Comment

by:vinhnl
ID: 13807019
System.Text.StringBuilder sb = new System.Text.StringBuilder();
0
 
LVL 6

Author Comment

by:PsychoDazey
ID: 13807065
Thanks Vin, I awarded you the points.  do you know what I would have to add to using for pageutility?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

830 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