ARTICLE

ASP.NET HTML Postback Custom Control

Posted by Mike Knuckey Articles | Visual Basic 2010 August 22, 2007
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.
Download Files:
 
Reader Level:

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.

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.NER 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.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor