How to get a reference to the control that caused a post back

Here is some old code I found (in VB.NET) the will tell you what control caused the post back.

  ' Return a reference to the control that caused the last postback,
'  even from the Page_Load event!
' It requires in input a reference to the posted-back page
' (this is necessary if you want to be able to move this function in a separate 
' class
' instead of in a page's codebehind class
' Example:
'    Private Sub Page_Load(ByVal sender As System.Object,
'  ByVal e As System.EventArgs) Handles MyBase.Load
'        Dim postbackCtl As Control = GetPostbackControl(Me)
'        If Not postbackCtl Is Nothing Then
'            lblResult.Text = postbackCtl.ID
'        End If
'    End Sub

Function GetPostbackControl(ByVal targPage As Page) As Control
    If targPage.IsPostBack Then
        ' try to find the name of the postback control in the hidden 
        ' __EVENTTARGET field
        Dim ctlName As String = targPage.Request.Form("__EVENTTARGET")
        ' if the string is not null, return the control with that name
        If ctlName.Trim().Length > 0 Then
            Return targPage.FindControl(ctlName)
        End If
        ' the trick above does not work if the postback is caused by standard 
        ' buttons.
        ' In that case we retrieve the control the ASP-way: by looking in the 
        ' Page's Form collection
        ' to find the name of a button control, that actually is the control 
        ' that submitted the page
        Dim keyName As String
        For Each keyName In targPage.Request.Form
            Dim ctl As Control = targPage.FindControl(keyName)
            ' if a control named as this key exists,
            '  check whether it is a button - if it is, return it!
            If Not ctl Is Nothing Then
                If TypeOf ctl Is Button Then
                    Return ctl
                End If
            End If
    End If

    Return Nothing
End Function
[tweetmeme only_single=”false”]

Generic handle error method

Here is a generic handle error method I put in my catches.  It will send an email with the inner exception unless it’s null then you get the exception message.  The stack trace is included.

You should the subject from “Application Name” to whatever your application is.
Also you should check out a previous post about a generic send mail method since this method calls that one.

private static void HandleError(Exception ex)
            String errorMessage = String.Empty;

            if (ex.InnerException != null)
                errorMessage = ex.InnerException.ToString();
                errorMessage = ex.Message.ToString();

            var trace = new System.Diagnostics.StackTrace(ex);

            StringBuilder errorBodyString = new StringBuilder();

            errorBodyString = errorBodyString.Append("Exception:" + errorMessage);
            errorBodyString = errorBodyString.Append("<br />");
            errorBodyString = errorBodyString.Append(trace);

String Subject = "Application Name";
            String Body = errorMessage;

           // sendNotification(Subject, errorBodyString.ToString());
[tweetmeme only_single=”false”]

Generic Send mail method

Here is a generic send mail method.

You just need to pass in a subject and body.
In your web.config you need a key for the from (notification_from), To (notification_to) and mail server (mail_server).
You could easily alter it to move the to (or from) to input variables.

private static void sendNotification(String Subject, String Body)
            MailMessage mailMessage = new MailMessage();
            SmtpClient smtpClient = new SmtpClient();
            string msg = string.Empty;

            String from = ConfigurationManager.AppSettings["notification_from"];
            String to = ConfigurationManager.AppSettings["notification_to"];
            String mailserver = ConfigurationManager.AppSettings["mail_server"];

            MailAddress fromAddress = new MailAddress(from);
            mailMessage.From = fromAddress;
            mailMessage.Subject = Subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = Body;

            smtpClient.Host = mailserver;

            smtpClient.EnableSsl = false;
            smtpClient.UseDefaultCredentials = true;

[tweetmeme only_single=”false”]

Ternary operator

The .NET framework has a cool feature called the Ternary Operator.  It’s basically a shortcut for  an if/then/else statement.

Here’s the format:
condition ? first_expression : second_expression;

The condition must be something that evaluates to true or false.  The first expression is what get’s returned if the condition is true.  Obviously the second expression gets returned if the condition is false.

I make use of then when I’m pulling data from a source and loading an object (ie, handle null from the source).

So here we have a normal check for null.  If it is null then set the property to String.Empty otherwise set to the value.

if (FirstName == null)

emp.FirstName =

      emp.FirstName = FirstName;

Here is the same statement using the new format.

emp.FirstName  = (FirstName == null) ? String.Empty : emp.FirstName = FirstName;

Much more compact code.

Remember you need both the ? and :

Don’t forget those.

[tweetmeme only_single=”false”]

Where to find where you click once app is deployed.

Click once applications have many advantages and disadvantages.

One of the disadvantages is that it hides where the application gets installed.  So if need to double check something like a connection string in your config file it can be a pain.

So next time you need to know where you application is this might help.

C:\Documents and Settings\[username]\Local Settings\Apps\2.0\[some
folder]\[some other folder]\manifests

Visual Studio 2008 web test recorder not working on 64-bit

The load test in Visual Studio are a great help to stress/performance test you applications.  With the web test you “should” be able to record actions as you use your application.  When you add a web test IE starts and there should be recorder bar on the left.


When ever I created a web test I never saw the web test recorder bar show up.  I didn’t even know I was missing it.  So, I was never able to get to do anything besides record that you launched a page which helps you test page load time, but doesn’t help you when your trying to determine the response time when clicking a button on your page.

After a little research (little as in there are few articles explaining this problem), I found a blog post explaining why the web test recorder not working.  It turns out to be a 64-bit issue.  There are some registry keys that need to be deleted, so IE will rebuild what explorer bars are available.

Here is the link to the article.

The short version is close all IE’s, run regedit, then delete the following keys

Discardable\PostSetup\Component Categories\{00021493-0000-0000-C000-000000000046}
Discardable\PostSetup\Component Categories\{00021494-0000-0000-C000-000000000046}

TFS 2008 get latest version automatically

When using  the Visual Studio integration for TFS 2008, you will notice that the latest version will not be retrieved when you start making changes to a file.

There is an option that you need to have an option.  To get to this option perform the following steps:

Launch Visual Studio 2008

Click Tools->Options

Expand the Source Control section

Click on Visual Studio Team Foundation Server

Ensure that the Get Latest version of item on check out is checked.

Now when you check out a file if you don’t have the latest version, it will prompt that you don’t and fetch it for you.

So you want to blog on

When I first started blogging I wanted to blog on  I have a friend who already blog’s on (David Mccollough) and I asked him what the steps were (because they are not on

Here is what he told me.

Well it’s a multistep process.

1st you have to create an account to be able to login, which I assume you have already done.

2nd you have to read the terms of use here. By the way there is nothing you have to sign here, unless they have changed it. I was kinda of confused at first, because I was looking for some way of saying that I had read them.

3rd Then you have to send an email to Joe Stagner, here is the link for that: Make sure you tell him that you agree to the terms of use that you read in step 2, you also have to send him your username from step 1. I think I also included who I was, who I worked for and that I had a desire to contribute to the .NET community.

4th Before you first post you need to read this:

Here is the original link to his blog post that outlines the process:

Can’t open a Visual Studio test project

The test tools in Visual Studio can help you stress test your application.  The only problem is that they are not included with the regular developer edition of Visual Studio.  So if have the wrong version installed you need to uninstall and reinstall a version that does have those project types (like Team Suite).

What I found though was after you uninstalled and reinstalled you were not able to open/create test projects.

The project types were there, but you receive an error:  The following project languages must be installed C#, C++, VB.NET (or something like that).  Everything else will work, but the test project template.

What you have to do in go to Control Panel –> Programs and Features (or add/remove programs) and do a Repair/Reinstall on your install of Visual Studio.
After that finishes you can now open/create test projects.