ASP.NET HTML Postback Custom Control

Web form controls do not offer a complete solution to ASP.Net design and hence HTML tags are still required. Occasionally the need exists for a postback from an HTML tag, this article presents a Custom Control to address this requirement.

HtmlPostScr.gif

Introduction

When using Map and Area tags, for instance, on a ASP.NET Web Form performing a postback to redirect the user to a new page is often needed. This can be addressed by using a User Control Hints and Tips or in the case of ASP.NET 2.0 new classes are available to address this requirement Server Event Handling in ASP.NET Web Pages. With ASP.NET 1.1 these classes are not available and using a Custom Control provides a very simple and compact solution.

Background

The control consists of two elements; event handler, output render.

The event handler is standard "Click/Onclick" event code which also includes a RaisePostbackEvent method to notify the control that it should handle an incoming postback event. This method extracts the event argument and saves it within the property variable htmlStr thereby enabling your code behind event method to read, test and take the appropriate action.

The render method outputs an anchor tag with no content text. Using the anchor tag ensures the inclusion of the _doPostBack javascript function while not having any content text between the tag pair means the anchor will not activate. The _doPostBack function can be called by any HTML tag using an onClick attribute and passing the custom control name,  HtmlPostback1, as the event target and a user specified string as the event argument.

Using the code

The above screen shot shows a simple test application of the Custom Control using a div tag to generate a postback and is included as part of the download code. The HtmlPostback Custom Control is dragged onto the web form, then as stated above a javascript call is made to the _doPostBack function by the required HTML tag.

A link is required between Custom Control event property htmlPost and the code behind event method. The test application code behind method HtmlPostback reads the argument string, ensures it is valid and then writes it to TextBox1 textbox.

Point of Interest

As indicated above this is a very simple Custom Control with very few lines of code. The interest and novelty is in the application which is described above.