473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Nov 19 '05 #1
14 8140
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.


Nov 19 '05 #2
Hello Brock, Thank you!!:)
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.


Nov 19 '05 #3
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.


Nov 19 '05 #4
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!)
"Laser Lu" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #5
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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #6
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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #7
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!)
"Laser Lu" <la******@hotmail.com> wrote in message
news:72********************@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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #8
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.
"Laser Lu" <la******@hotmail.com> wrote in message
news:49********************@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.

Nov 19 '05 #9
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" <la******@hotmail.com> wrote in message
news:72********************@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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #10
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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #11
In your code behind, it is "this" or "Me".

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Laser Lu" <la******@hotmail.com> wrote in message
news:49********************@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.

Nov 19 '05 #12
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:)
In your code behind, it is "this" or "Me".

"Laser Lu" <la******@hotmail.com> wrote in message
news:49********************@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.


Nov 19 '05 #13
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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #14
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" <la******@hotmail.com> wrote in message
news:12*********************@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" <la******@hotmail.com> wrote in message
news:68********************@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.


Nov 19 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Laser Lu | last post by:
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
0
by: Jeff Dockman | last post by:
Background: We needed the ability to create a templated web site that may change in layout from installation to installation. To accomplish this, we created a base Page class that all pages in...
2
by: Jiho Han | last post by:
In CreateChildControls method of my Page, I am trying to see if a hidden input field is present (by id) and if not, add it to a predefined form (by id). protected override void...
0
by: VB Programmer | last post by:
I have 2 webforms. When I print out the page.Controls(1).tostring variable one says "HtmlForm", the other "HtmlGenericControl". How can I change the first one to "HtmlForm"? ?...
2
by: Shawn | last post by:
Hi. I'm trying to loop through all webcontrols in my System.Web.UI.HtmlControls.HtmlForm. This is my code: Dim form As HtmlForm Dim control As Control form = Me.FindControl("completion")...
0
by: Wouter vanEck | last post by:
Hi All, I have the following problem, I have a string for example "<table><tr></td><MyTextBox /></td></tr></table>" which is created by a user typing in a textbox. Now I like to take that...
0
by: Bruno | last post by:
Hello guys, I'm having problem using tagMapping with HtmlForm, I have this in web.config: <tagMapping> <add tagType="System.Web.UI.HtmlControls.HtmlForm" mappedTagType="CustomForm" />...
3
by: Roshawn | last post by:
Hi, I have a control that derives from the HtmlForm control. The difference is that I can set the action attribute to whatever I desire. This works fine. :-) What I can't seem to do is...
1
by: mwalts | last post by:
Hello all, I have to write a web part for a SharePoint 2003 server. We only have one SharePoint server, so developing directly on it isn't really viable. So I've planned to do the majority of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.