Monthly Archives: March 2008

ASP.Net defaultfocus and defaultbutton in Master Pages

One of the big problems I faced in .Net 1.1 web applications was capturing the enter key so that the correct asp:button would be “clicked.”

To solve this problem, ASP.Net 2.0 lets you specify this on the html form element with the defaultfocus and defaultbutton attributes. This is really slick and works like a charm.

But what if you want to user a master page and specify the default focus or button from your content page?

In your code behind you can simply put:

    Page.Form.DefaultFocus = txtFirstName.ClientID;
Page.Form.DefaultButton = btnSave.UniqueID;

The catch here is to remember that when the page is rendered, your controls’ ids will be prefixed because they’re in a naming container. So make sure you use the ID fields that will be correct at that time (ClientID and UniqueID).

Generating numbers from a Normal (Gaussian) Distribution

Most programming languages have built in libraries for generating random numbers from a uniform distribution. If you need to convert this to a normal or Gaussian distribution, some sort of transformation must be made.

I found a good page that talks about this process and gives some example code in how to convert a uniform distribution to a Gaussian.

http://www.taygeta.com/random/gaussian.html

Here is the .Net (C# 2.0) code I have adpated from this page. This function returns a random number with a mean of 0 and a standard deviation of 1.

public static double GetNormalizedRandomNumber()
{
	double x1, x2, w, y1, y2;

	do
	{
		x1 = 2.0 * RandomGenerator.NextDouble() - 1.0;
		x2 = 2.0 * RandomGenerator.NextDouble() - 1.0;
		w = x1 * x1 + x2 * x2;
	} while (w >= 1.0);

    w = Math.Sqrt((-2.0 * System.Math.Log(w, System.Math.E)) / w);    y1 = x1 * w;    y2 = x2 * w;

    return y1;
}