Access a Form Control in Code Behind


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.


Similar Articles