How to get the HtmlForm element of an ASP.NET Page?

  • Thread starter Thread starter Laser Lu
  • Start date Start date
L

Laser Lu

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
 
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?
 
Try:

Page_Load
'vb.net
dim form as HtmlForm = ctype(FindControl("form1"), HtmlForm)

//c#
HtmlForm form = (HtmlForm)FindControl("form1");
end

where "form1" is the id you've given to your form tag (ie, <form id="form1"
runat="server" > )

If that's still to "hard-coded" for you...you'll need to loop through the
page controls and look for the control

public function GetPageForm(byval parentControl as Control) as HtmlForm
for each child as Control in parentControl.Controls
if typeof child is HtmlForm
return ctype(child, htmlForm)
end if
next
return nothing
end function

you might need to make the above code recursive though...if the htmlform is
embedded (unlikely)..

anyways...seems like overkill...Brock's suggestion should be the right way
to go unless there are very special circumstances..

Karl

P.S. - Hi Brock :)
 
Hello Karl,
Nice approach;) It seems that I have to use the GetPageForm function provided
by you, as the form's id is not deterministic at run time.
Thank you!
 
I was puzzled that why not the System.Web.UI.Control / Page class provide
such a method like FindForm() which is provided in System.Windows.Forms.Control
class, as the Form element is specific and unlackable to all ASP.NET Web
Forms. It's indeed a little bit unconvenient without such a method or property
when writing some ASP.NET code:(
 
Laser:
HtmlForm isn't 100% necessary...you can easily have an ASP:Label in a page
without a form...there are other controls which don't require a form also,
though most functionality does require it.

by the way, in the future youshould try to avoid cross-posting to so many
newsgroups...

Karl
 
you can use the Page.FindControl method, e.g.

System.Web.UI.HtmlControls.HtmlForm form = Page.FindControl("Form1") as
System.Web.UI.HtmlControls.HtmlForm;


--
HTH

Ollie Riches
http://www.phoneanalyser.net

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a programmer
helping programmers.
 
Ok, Karl, thank you:)
I'll remember not to cross-posting to too many newsgroups from now on:)
 
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
 
Hello Robbe Morris,
Maybe I didn't say it clearly. The object I want to get is of type HtmlForm,
which is exactly refer to the HTML <FORM> element in the Web Page, but not
the Page object itself. So here, I suppose, it is not 'this' or 'me' in the
code behind:) However, thanks for your reply:)
 
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?
 
Excellent point! I missed that. The algorithm below will do a depth first
search. Normally this doesn't matter as the Page.Controls looks like

LiteralControl
HtmlForm
LiteralControl

The first literal control will have no children, nor will the third control.
So you will take two iterations to find the HtmlForm.

bill


Laser Lu said:
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.

 
Back
Top