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

Discussion in 'ASP .Net Building Controls' started by Laser Lu, May 3, 2005.

  1. Laser Lu

    Laser Lu Guest

    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
    Laser Lu, May 3, 2005
    #1
    1. Advertising

  2. Laser Lu

    Brock Allen Guest

    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.
    Brock Allen, May 3, 2005
    #2
    1. Advertising

  3. Laser Lu

    Laser Lu Guest

    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.
    Laser Lu, May 3, 2005
    #3
  4. Laser Lu

    Laser Lu Guest

    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.
    Laser Lu, May 3, 2005
    #4
  5. Laser Lu

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > 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.

    >
    >
    >
    Karl Seguin, May 3, 2005
    #5
  6. Laser Lu

    Laser Lu Guest

    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" <> wrote in message
    > news:...
    >
    >> 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.
    Laser Lu, May 3, 2005
    #6
  7. Laser Lu

    Laser Lu Guest

    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" <> wrote in message
    >> news:...
    >>
    >>> 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.
    Laser Lu, May 3, 2005
    #7
  8. Laser Lu

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    >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" <> wrote in message
    >>> news:...
    >>>
    >>>> 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.

    >
    >
    >
    Karl Seguin, May 3, 2005
    #8
  9. Laser Lu

    Ollie Riches Guest

    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" <> wrote in message
    news:...
    > 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.
    >
    >
    Ollie Riches, May 3, 2005
    #9
  10. Laser Lu

    Laser Lu Guest

    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" <> wrote in message
    > news:...
    >
    >> 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" <> wrote in message
    >>>> news:...
    >>>>
    >>>>> 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.
    Laser Lu, May 3, 2005
    #10
  11. 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

    "Laser Lu" <> wrote in message
    news:...
    > 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.

    >
    >
    >
    William F. Robertson, Jr., May 3, 2005
    #11
  12. 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" <> wrote in message
    news:...
    > 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.
    >
    >
    Robbe Morris [C# MVP], May 4, 2005
    #12
  13. Laser Lu

    Laser Lu Guest

    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" <> wrote in message
    > news:...
    >
    >> 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.
    Laser Lu, May 4, 2005
    #13
  14. Laser Lu

    Laser Lu Guest

    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
    >
    > "Laser Lu" <> wrote in message
    > news:...
    >
    >> 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.
    Laser Lu, May 4, 2005
    #14
  15. 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" <> wrote in message
    news:...
    > 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
    > >
    > > "Laser Lu" <> wrote in message
    > > news:...
    > >
    > >> 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.

    >
    >
    >
    William F. Robertson, Jr., May 4, 2005
    #15
  16. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > > news:...
    > >
    > >> 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" <> wrote in message
    > >>>> news:...
    > >>>>
    > >>>>> 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.

    >
    >
    >
    Fred Hirschfeld, May 7, 2005
    #16
  17. Laser Lu

    Laser Lu Guest

    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:)

    > 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" <> wrote in message
    > news:...
    >
    >> 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" <> wrote in message
    >>> news:...
    >>>
    >>>> 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" <> wrote in message
    >>>>>> news:...
    >>>>>>
    >>>>>>> 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.
    Laser Lu, May 10, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Dockman
    Replies:
    0
    Views:
    427
    Jeff Dockman
    Nov 17, 2003
  2. VB Programmer
    Replies:
    0
    Views:
    369
    VB Programmer
    Mar 4, 2004
  3. VB Programmer
    Replies:
    0
    Views:
    390
    VB Programmer
    Mar 5, 2004
  4. Laser Lu
    Replies:
    14
    Views:
    10,339
    William F. Robertson, Jr.
    May 4, 2005
  5. Laser Lu

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

    Laser Lu, May 3, 2005, in forum: ASP .Net Web Controls
    Replies:
    16
    Views:
    294
    Laser Lu
    May 10, 2005
Loading...

Share This Page