Hello William, thank you
As you said, since the HtmlForm element is typically a top level control,
or not-too-deep level control, the algorithm taken for finding the Form element
should search horizontally through a topper level first and then its child
levels, not deeply search the children first using function recursion. Do
you know what I mean? Am I right?
You can iterate the controls on the page to return the form control.
This is a recusive call to step through each Control in the
ControlCollection looking for the HtmlForm.
public static HtmlForm GetForm( ControlCollection controls )
{
HtmlForm form = null;
for( int i = 0 ; i < controls.Count ; i++ )
{
if ( controls
is HtmlForm ) return controls as HtmlForm;
if ( controls.HasControls() )
{
form = GetForm( controls.Controls );
if ( form != null ) return form;
}
return null;
}
Since the HtmlForm is typically a top level control, performance isn't
too terribly bad. I didn't actually test this code, but it should
work similiar to this.
HtmlForm myForm = GetForm( Page.Controls );
HTH,
bill
However, is there any alternative approach which needs not to do hard
coding?
I mean how to get the Form element at runtime without the declaration
of HtmlForm member?
You need to declare a field in your code behind for the form:
protected System.Web.UI.HtmlControls.HtmlForm Form1;
Use Form1 or whatever ID you've assigned to it in the ASPX.
-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello, All,
I just want to know that generally how to get the HtmlForm element
in
an
ASP.NET Page?
Can anybody help? Please
Best regards,
Laser Lu.