ASP.NET, Technology

A generic FindControl extension method

In order to clean up and make our code more readable, generics and extension methods comes in very handy. In ASP.NET, the Control class has a FindControl method which is used as follows:

((TextBox) Page.FindControl("TxtName")).Text = "Some name";

Since this method returns a Control type, we must cast it to it’s actual type before we can access any of it’s members. A way to improve this code, is to create a new generic FindControl method. Since all the controls in ASP.NET such as Page, Button, TextBox, inherits from the Control class, we can make this method an extension method. By doing this, our method can be accessed the same way as the non-generic FindControl method:

Page.FindControl<TextBox>("TxtName").Text = "Some name";

This code is tidier and easier to read. Here is the code for the extension method:

public static class ControlExtensions
{
	public static T FindControl<T>(this System.Web.UI.Control control, string id)
		where T : System.Web.UI.Control
	{
		return (T) control.FindControl(id);
	}
}
ASP.NET, Technology

Always wire your events in the code behind, NOT in the aspx/ascx template file

With ASP.NET you can wire your events in the aspx/ascx template file or in the code behind file. If you wire your events in the template file, you will not get compile time support. This means that if a method is renamed or removed without updating the template file, the compiler will not throw an error. However, if the event is wired in the code behind, an error will be thrown. Events should therefore always be wired in the code behind.

Template file event wiring - not recommended:

<asp:Button runat="server" ID="BtnSave" OnClick="Save" Text="Save" />

Code behind event wiring - recommended:

protected override void OnInit(EventArgs e)
{
	base.OnInit(e);

	BtnSave.Click += new EventHandler(Save);
}
ASP.NET, Technology

Placing HTML elements on top of the ModalPopupExtender control

If you want a HTML element to always be on top of the ModalPopupExtender control, give the element the CSS z-index value 99999998.

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");
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);
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>
ASP.NET, Technology

Create a global progress bar for your ASP.NET AJAX applications

In ASP.NET AJAX you can use the UpdateProgress control to display a progress bar for asynchronous page updates. This control needs to be attached to a single UpdatePanel. If you need a consistent progress bar throughout your application, you will thus need to add one UpdateProgress control for every UpdatePanel control. Since this is not an ideal solution, then instead of using the UpdateProgress control, we can create our own progress bar by attaching a method to the beginRequest and endRequest event of the PageRequestManager class, which toggles the visibility of a div-container with an animated image. Have a look at the example below. Read More »

ASP.NET, SQL Server, Technology

How to store files in a MS-SQL Server database using ASP.NET

In this post we will take a look at how to store data in a MS-SQL Server database using ASP.NET. We will start by creating the database table in which we will store the actual file data, and then create a new Web Form with a file upload control and an upload button which will save the uploaded file to the database. After that we need to create a generic HTTP Handler which will be responsible for flushing out the file to the client. The reason to why we will use a HTTP Handler for this is that we don’t need all the functionality that the System.Web.UI.Page provides, so having both performance and simplicity in mind a HTTP Handler is a better choice. Read More »

ASP.NET, Technology

Export data to MS Excel using ASP.NET

The simplest way to export data to Microsoft Excel using ASP.NET is to make a tab- and line break delimited string (CSV), which is written out to the client using a HTTP-Handler with the content type set to “application/vnd.ms-excel”. Read More »