SIGN UP MEMBER LOGIN:    
ARTICLE

Access a Form Control in Code Behind

Posted by Chandra Vedantham Articles | How do I March 14, 2006
The code given in article will serve as a BasePage which can be used in the ASPX pages where HtmlForm controls needs to be accessed
Reader Level:

This is a small article which describes small code snippet which can be used to get access to the Html Form Control of an Aspx Page.

Given below is code for a class by name BasePage.

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace HandlerTest

{

public class BasePage:Page

{

private HtmlForm _pageform = null;

protected HtmlForm PageForm

{

get

{

return _pageform;

}

}

protected override void AddedControl(Control control, int index)

{

if(control is HtmlForm)

{

_pageform = control as HtmlForm;

}

base.AddedControl (control, index);

}

protected bool HasForm

{

get

{

return (_pageform != null);

}

}

 

}

}

This class is derived from the Page class of System.Web.UI.WebControls namespace.

Here we are overiding the AddedControl method of the base class and verfying if the control being added is a HtmlForm control and if it is then we are assigning the control to the _pageform variable.

Also the property PageForm allows us to access the Form Control from any class derived from the BasePage Class.

Login to add your contents and source code to this article
share this article :
post comment
 

Yes, I see the idea... But I bet it would have been easier to read the code if it was made a bit more readable... At least I prefere if the code is properly indented... And that "using" statements that are not used anyway, are removed, it make it easier to see what it is working with... And some comment (You know, lines starting with //) describing the thoughts behind the code, is nice to have...

 

Posted by lod Mar 31, 2006
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    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!
Team Foundation Server Hosting
Become a Sponsor