Common Tasks in Developing ASP.NET Application


I mentioned developing an ASP.NET website in the first blog. ASP.NET is a sophisticated architecture that often confuses developers, especially which event fires first could be difficult to expect. You should understand ASP.NET Page Life Cycle before you start dragging WebControl to the page.


Task 1: Impersonation


Often times, you need to impersonate as a specific active directory user to connect to a server, check service status, or access MSMQ. Microsoft Support has a very good example page about how to impersonate as a specific user. You can change the impersonate method and undo impersonation method to be public static to allow all other methods in your application to use them. Below is the code showing how to consume the impersonation methods:


ServiceStatus.impersonated = ServiceStatus.impersonateValidUser("ServiceAccountUser","Domain","Don’tTell");


if (!ServiceStatus.impersonated)


{


    // impersonation failed. Do not continue to call Administration web service


string message = DateTime.Now + " - Impersonaion failed.”


Trace.WriteLineIf(ServiceStatus.debugSwitch.TraceError, message);


    throw new Exception(message);


}


Task 2: Start application thread


The static constructor is the best place you can start and manage a thread. The thread will exist in the scale of the application regardless of how many page instances there are at any moment. It is a useful practice if you need to run long haul SQL queries or you need to check Windows Service status every 10 minutes alongside your ASP.NET application.
// There should always be 1 static instance of ServiceStatus executing


// method ServiceStatus.StartChecking in the application pool


static _Default()


{


    if (_Default.logEnvStatus == null || !_Default.logEnvStatus.IsAlive)


    {


        _Default.logEnvStatus = new Thread(new ThreadStart(ServiceStatus.StartChecking));


        _Default.logEnvStatus.Start();


    }


    _Default.refreshTime = ServiceStatus.appSettings["RefreshTime"];


    _Default.MaxLatencyAllowed = int.Parse(ServiceStatus.appSettings["MaxLatencyAllowed"]);


}


Task 3: Access DetailsView or GridView Rows


The DetailsView class bound to a data source is convenient to display data from a SQL database table. However, we don’t have much control over the data on the rows at runtime. In the DataBound event of DetailsView class, you can use the following code to access text on each row and modify the row’s color:


System.Data.DataRowView rowView = (System.Data.DataRowView)this.EndpointDetailsView.DataItem;
if (rowView != null)


{
    for (int i = 0; i < rowView.Row.ItemArray.Length; ++i)
    {
        string endpoint = rowView.Row[i].ToString();
        if (endpoint.IndexOf("http") == 0)
        {
            // text in the row’s cell has string http
            ControlCollection c = this.EndpointDetailsView.Controls;
            DetailsViewRow row = this.EndpointDetailsView.Rows[i];
            // change the row color
            row.BackColor = System.Drawing.Color.LightGreen;


The same technique on GridView’s RowDataBound event:


const int latencyIndex = 5;


const int EnvIDIndex = 1;


if (e.Row.RowType == DataControlRowType.DataRow)


{


    int latency = int.Parse(e.Row.Cells[latencyIndex].Text);


    if (latency > _Default.MaxLatencyAllowed)


    {


        e.Row.Cells[latencyIndex].ForeColor = System.Drawing.Color.Red;


        e.Row.ToolTip = _Default.LatencyHighMessage;


    }


The only drawback is you need to know the index of the cell data to manipulate it. In the example code above, latencyIndex is a number. If you miscalculated the index and the string is not a number, int.Parse will throw a format exception.

Skip to main content