ASP.NET, Technology

A recursive FindControl extension method

The System.Web.UI.Control class has a FindControl method for finding a control by its ID. This method searches for a given control in the first level child controls, and not in any child controls of a child. In order to do the latter, a custom method must be implemented. Below is a generic, recursive, extension method that does this:

public static class ControlExtension
{
	public static T FindControlRecursive<T>(this Control container, string id)
			where T : Control
	{
		if (container.HasControls())
		{
			T foundControl = null;

			foreach (Control control in container.Controls)
			{
				if (control.ID == id && control is T)
					foundControl = (T) control;
				else
					foundControl = FindControlRecursive<T>(control, id);

				if (foundControl != null)
					return foundControl;
			}
		}

		return null;
	}
}

Usage:

var control = Page.FindControlRecursive<TextBox>("TxtName");
.NET, Technology

CSV export to MS Excel, SYLK file error

I just received the following error when creating a CSV formatted file of some report data, and opening it in Microsoft Excel:

Excel has detected that 'xxx.xls', is a SYLK file. Either the file has errors or it is not a SYLK file format. Click OK to try to open the file in a different format.

After some research it turns out this problem occurs if the first two letters in the file content is ID (in uppercase). The only way to get rid of this error message is therefore to not use ID in uppercase (Id, id is ok), or change it to something else.

ASP.NET, Technology

How to disable all the ASP.NET input controls within a container control

The following method will recursively disable all the input controls within a container control such as a User Control, PlaceHolder, Panel etc.:

public void DisableInputControls(Control container)
{
	foreach (Control control in container.Controls)
	{
		if (control is TextBox
			|| control is DropDownList
			|| control is RadioButtonList
			|| control is RadioButton
			|| control is CheckBoxList
			|| control is CheckBox
			|| control is Button)
		{
			(control as WebControl).Enabled = false;
		}

		if (control.Controls.Count > 0)
			DisableInputControls(control);
	}
}

Example usage:

DisableInputControls(MyPlaceHolder);
.NET, Technology

How to generate the SHA-1 hash of a string using C#

The namespace System.Security.Cryptography contains classes for working with cryptography such as hashing. Within this namespace you will find the SHA1Managed class which we can utilize to generate the SHA-1 hash of a string. Below you will find a helper method using this class:

public static string GetSha1(string value)
{
	var data = Encoding.ASCII.GetBytes(value);
	var hashData = new SHA1Managed().ComputeHash(data);

	var hash = string.Empty;

	foreach (var b in hashData)
		hash += b.ToString("X2");

	return hash;
}
SQL Server, Technology

MS SQL-Server - How to select numeric rows from a varchar column

Luckily T-SQL has an ISNUMERIC(value) function:

select ISNUMERIC('PostalCode123') -- Returns 0
select ISNUMERIC('123') -- Returns 1

Therefore:

select *
from Customer
where ISNUMERIC(PostalCode) = 1

…returns only the rows where the varchar column PostalCode is numeric.

.NET, Technology

Why you should not use the ADO.NET Entity Framework

Note: This post is about ADO.NET Entity Framework version 1.0. Newer versions of the framework might have been improved or solved any of the issues mentioned below.

For some days now I’ve been using the ADO.NET Entity Framework on a pet project of mine. My conclusion so far is that it’s useless in most scenarios, and that I’m lucky to not use it on a real business application. Read More »

.NET, Technology

How to get rid of the alert “The following module was built either with optimizations enabled or without debug information”

If you have a solution in Visual Studio which consists of both web- and class library projects, then you might get an alert box with the following message:

"The following module was built either with optimizations enabled or without debug information:

....

To debug this module, change its project build configuration to Debug mode. To suppress this message, disable the 'Warn if no user code on launch' debugger option."

Do this to get rid of the alert:
1. Right click the project giving the alert, choose “Properties”.
2. Choose “Build” and then “Advanced…”
3. Set Debug info to “full”.

Now the alert should be gone.

ASP.NET, Technology

Response.Redirect not working within an UpdatePanel?

If you get an exception when doing a Response.Redirect within an UpdatePanel then you are most probably missing the following lines in your web.config:

<httpModules>
  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
JavaScript, Technology

FCKEditor 2.x: How to add a new button with custom javascript to the toolbar

In this tutorial we will have a look at how to add a new button with custom javascript to a FCKeditor toolbar. In this example our custom button will simply open a link in a new window when it’s clicked. Read More »

JavaScript

FCKeditor 2.x: How to get an instance of the editor using javascript

This is how you get an instance of the editor when you are within the page that loads the editor: Read More »

« Newer PostsOlder Posts »