This article contains the short answer to some of the frequently asked questions we all see on the many developer email lists. ASP.NET and C# have brought many developers from the C++/MFC world into the web application arena. Some of these programmers may be very experienced, but with little exposure to some of the web basics. Others are just beginners that have a lot of questions, and are sometimes afraid to ask for help.
So, in no particular order (for now), here is installment #1 of some common question and a few little tid-bits.
Q: How do I set focus to a control from my C# server code?
You can't. Well, not directly. Setting focus is still a client-browser function and you'll need to use client scripting. There are several techniques; here's a simple one.
However, if you do want to create this kind of script from your server code, here's a little method I use to programatically generate the script to set focus to any server control. The input to this method is defined as the base class of all server Controls, so you can pass any Web Server Control or HTML Server Control to this method.
private void SetFocus( System.Web.UI.Control ctrl )
"document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>";
RegisterStartupScript("focus", s );
The RegisterStartupScript method places this script block at the bottom of your rendered HTML page.
Q: Is there a native function in .NET to repeat a string?
Yes. Look in the System.Text namespace and check out the StringBuilder class. Among others methods, it includes:
public StringBuilder Insert(
Q: Why won't my password textbox show an initial value?
The Asp:TextBox control with the TextMode property set to "Password" will not show anything when you set the Text property, not even the string of asterisks. This is probably a good thing; you should provide a special "change your password" function of some kind. However, if you have a user profile page and don't want the user to have to enter the password unless they are making changes, you can get the asterisks to dislay, and retrieve the Text property on a postback by setting the "value" attribute of the control as follows.
PasswordCtrl.Attributes["value"] = "thepassword";
This also applies the HTML Input control when you specify "runat=server".
Q: How do I get my connection string from Web.config?
This is an easy one; but the question comes up a lot. The key may be to remember, this is case sensitive.
<ADD key="dsn" value="Provider=SQLOLEDB;Data Source=CRUNCH;Database=vfc;User Id=sa;" />
This C# method uses the connection string from Web.config to create an OleDbConnection.
public static OleDbConnection GetConnection()
OleDbConnection db = new OleDbConnection();
db.ConnectionString = ConfigurationSettings.AppSettings["dsn"];
Q: Where is my site?
I often need to know if my application is running on my workstation or on a production server. I've always used a statement like:
if ( HttpContext.Current.Server.MachineName.ToLower() == "fins" )
... do my local thing
where "fins" happens to be the name of my local machine. This failed when I reloaded my workstation recently (don't ask) and I changed the name. I realized that in most cases what I really needed to know was whether or not my application was running as a subweb (as it is on localserver), or as a production web site. In this case, I now call a routine that checks the root path of my web application. Note here the "~" reference with some extra scrbible to insure it will match the root path mapped from "/" if it is not a subweb.
public static bool IsSubWeb()
return ( HttpContext.Current.Request.MapPath("~/.\\") !=
There is a good article by Remas Wojciechowski on the tilde character (~) reference - See article.