Home All Groups Group Topic Archive Search About

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

Author
3 May 2005 12:25 AM
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.

Author
3 May 2005 1:28 AM
Brock Allen
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



Show quoteHide quote
> 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.
Author
3 May 2005 1:49 AM
Laser Lu
Hello Brock, Thank you!!:)

Show quoteHide quote
> 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.
Author
3 May 2005 1:50 AM
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?

Show quoteHide quote
> 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.
Author
3 May 2005 1:59 AM
Karl Seguin
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 :)

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:6810632507105929531250@news.microsoft.com...
> 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.
>
>
>
Author
3 May 2005 2:22 AM
Laser Lu
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!

Show quoteHide quote
> 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 :)
>
> "Laser Lu" <laser***@hotmail.com> wrote in message
> news:6810632507105929531250@news.microsoft.com...
>
>> 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.
Author
3 May 2005 2:47 AM
Laser Lu
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:(

Show quoteHide quote
> 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!
>> 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 :)
>>
>> "Laser Lu" <laser***@hotmail.com> wrote in message
>> news:6810632507105929531250@news.microsoft.com...
>>
>>> 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.
Author
3 May 2005 11:00 AM
Karl Seguin
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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:7215632507140157968750@news.microsoft.com...
>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:(
>
>> 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!
>>> 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 :)
>>>
>>> "Laser Lu" <laser***@hotmail.com> wrote in message
>>> news:6810632507105929531250@news.microsoft.com...
>>>
>>>> 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.
>
>
>
Author
3 May 2005 1:41 PM
Laser Lu
Ok, Karl, thank you:)
I'll remember not to cross-posting to too many newsgroups from now on:)

Show quoteHide quote
> 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
>
> "Laser Lu" <laser***@hotmail.com> wrote in message
> news:7215632507140157968750@news.microsoft.com...
>
>> 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:(
>>
>>> 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!
>>>> 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 :)
>>>>
>>>> "Laser Lu" <laser***@hotmail.com> wrote in message
>>>> news:6810632507105929531250@news.microsoft.com...
>>>>
>>>>> 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.
Author
7 May 2005 10:37 PM
Fred Hirschfeld
I would suggest that you have a base class that derives from
System.Web.UI.Page so you can have some generic methods that you would like.
Then I would create a method similar to the one outlined previously with one
exception, make the method look for the Controls form... This way you can
get the form that a control belongs to since there could be nested forms or
multiple forms on a page.

Fred


Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:10360632507532452031250@news.microsoft.com...
> Ok, Karl, thank you:)
> I'll remember not to cross-posting to too many newsgroups from now on:)
>
> > 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
> >
> > "Laser Lu" <laser***@hotmail.com> wrote in message
> > news:7215632507140157968750@news.microsoft.com...
> >
> >> 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:(
> >>
> >>> 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!
> >>>> 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 :)
> >>>>
> >>>> "Laser Lu" <laser***@hotmail.com> wrote in message
> >>>> news:6810632507105929531250@news.microsoft.com...
> >>>>
> >>>>> 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.
>
>
>
Author
10 May 2005 10:51 AM
Laser Lu
Thank you, Fred! This is exactly a good approach leveraging inheritance:)
By the way, is it possible to have nested forms or multiple forms on an ASP.NET
page? I'm afraid not, and you may have a check:)

Show quoteHide quote
> I would suggest that you have a base class that derives from
> System.Web.UI.Page so you can have some generic methods that you would
> like. Then I would create a method similar to the one outlined
> previously with one exception, make the method look for the Controls
> form... This way you can get the form that a control belongs to since
> there could be nested forms or multiple forms on a page.
>
> Fred
>
> "Laser Lu" <laser***@hotmail.com> wrote in message
> news:10360632507532452031250@news.microsoft.com...
>
>> Ok, Karl, thank you:)
>> I'll remember not to cross-posting to too many newsgroups from now
>> on:)
>>> 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
>>>
>>> "Laser Lu" <laser***@hotmail.com> wrote in message
>>> news:7215632507140157968750@news.microsoft.com...
>>>
>>>> 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:(
>>>>
>>>>> 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!
>>>>>> 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 :)
>>>>>>
>>>>>> "Laser Lu" <laser***@hotmail.com> wrote in message
>>>>>> news:6810632507105929531250@news.microsoft.com...
>>>>>>
>>>>>>> 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.
Author
3 May 2005 2:23 PM
William F. Robertson, Jr.
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[i] is HtmlForm ) return controls[i] as HtmlForm;
    if ( controls[i].HasControls() )
    {
      form = GetForm( controls[i].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

Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:6810632507105929531250@news.microsoft.com...
> 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.
>
>
>
Author
4 May 2005 1:35 AM
Laser Lu
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? :)

Show quoteHide quote
> 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[i] is HtmlForm ) return controls[i] as HtmlForm;
> if ( controls[i].HasControls() )
> {
> form = GetForm( controls[i].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
>
> "Laser Lu" <laser***@hotmail.com> wrote in message
> news:6810632507105929531250@news.microsoft.com...
>
>> 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.
Author
4 May 2005 1:26 PM
William F. Robertson, Jr.
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


Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:12441632507960828593750@news.microsoft.com...
> 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[i] is HtmlForm ) return controls[i] as HtmlForm;
> > if ( controls[i].HasControls() )
> > {
> > form = GetForm( controls[i].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
> >
> > "Laser Lu" <laser***@hotmail.com> wrote in message
> > news:6810632507105929531250@news.microsoft.com...
> >
> >> 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.
>
>
>
Author
3 May 2005 1:38 PM
Ollie Riches
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.


Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:4904632507054740937500@news.microsoft.com...
> 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.
>
>
Author
4 May 2005 12:41 AM
Robbe Morris [C# MVP]
In your code behind, it is "this" or "Me".

Show quoteHide quote
"Laser Lu" <laser***@hotmail.com> wrote in message
news:4904632507054740937500@news.microsoft.com...
> 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.
>
>
Author
4 May 2005 1:14 AM
Laser Lu
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:)

Show quoteHide quote
> In your code behind, it is "this" or "Me".
>
> "Laser Lu" <laser***@hotmail.com> wrote in message
> news:4904632507054740937500@news.microsoft.com...
>
>> 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.