Tid-bits and ASP.NET Newbie FAQs Part 1
page 1 of 1
Published: 20 Oct 2003
Unedited - Community Contributed
Abstract
This article contains the short answers to some of the ASP.NET frequently asked questions seen on many of developer email lists.
by Steve Sharrock
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 18032/ 35

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.

I don't know the source, but I've heard the statement: "The only bad question is the question that you don't ask." So stay on those lists, keep asking question and keep learning. Use the items below to get a lot of little answers in one place. I'll try and keep this article updated on a regular basis. I know many of these questions have been answered in depth in other articles on AspAlliance and other sites. When I know about them, I'll provide links to those references also. I'm sorry I won't be showing much VB; C# and JavaScript are my tools at this time.

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. 

<SCRIPT language="javascript">
 document.forms[0].elements["MyEdit"].focus()
</SCRIPT> 

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 )
{
  string s = "<SCRIPT language=\"javascript\">" +
    "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(
   int index,
   string value,
   int count
);

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.

<appSettings>
  <ADD key="dsn" value="Provider=SQLOLEDB;Data Source=CRUNCH;Database=vfc;User Id=sa;" />
</appSettings>

This C# method uses the connection string from Web.config to create an OleDbConnection.

using System.Configuration;
public static OleDbConnection GetConnection()
{
  OleDbConnection db = new OleDbConnection();
  db.ConnectionString = ConfigurationSettings.AppSettings["dsn"];
  return db;
}

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("~/.\\") !=
          HttpContext.Current.Request.MapPath("/") );
}

There is a good article by Remas Wojciechowski on the tilde character (~) reference - See article.



User Comments

Title: Thanks   
Name: Nathan Prather
Date: 2010-01-19 9:51:40 AM
Comment:
thanks for the password post.
I'm using it now!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-12-17 1:11:44 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search