Can I have GET and POST forms on the same page?

A

Alan Silver

Hello,

From my limited experience with ASP.NET (couple of weeks so far, much
time wasted with stupid book), you can only have one form per page. If
so, how do you handle the scenario where you want two forms (from the
user's point of view) on a page, one form that uses POST and one that
uses GET?

For example, suppose your site has a members' log-in form at the top of
every page. This would normally use POST as you don't want the password
appearing in the URL where it can be seen in the history. However,
search engines on sites generally use GET as this makes bookmarking the
page easy.

Can you do this? If not, is there any way around it?

TIA
 
M

Marina

Typically, in ASP.NET each form just posts to itself. This is called a
postback. Every form can process it's own information, you do not post to
another page, as you did in ASP.

In Page_Load you can check the IsPostBack property to see if the page is
running as a result of a postback.

Additionally, since everything is completely OO, a button is an object on
the server with events. So you can handle the click even of a particular
button by writing a handler for its Click event.
 
S

Steve C. Orr [MVP, MCSD]

You can only have one visible server form on the page at a time.
So you could have more than one server form as long as you only show one at
a time.
Or you can have more than one form visible on your page at a time, but only
one of them can be a server form (i.e. with the runat=server attribute.)
Server forms only support posting back to themselves (not to other pages.)

This will all become more flexible in .NET version 2.
 
A

Alan Silver

Typically, in ASP.NET each form just posts to itself. This is called a
postback. Every form can process it's own information, you do not post to
another page, as you did in ASP.

Thanks, I understand this, but you still sometimes want to post to
another page. A log-in form that appears on every page is a good
example. You wouldn't bother adding the log-in code to every page, just
have one page to handle it.

Anyway, this doesn't really answer the question. Even if you were to
post the log-in back to the same page, you would still want the log-in
to use POST and the search engine to use GET. That was the point of my
question.
In Page_Load you can check the IsPostBack property to see if the page is
running as a result of a postback.

Additionally, since everything is completely OO, a button is an object on
the server with events. So you can handle the click even of a particular
button by writing a handler for its Click event.

Thanks, I understand these points as well, but don't see how they relate
t my question.

Thanks for the reply, but I'm still not clear. I am asking specifically
about using POST and GET on the same page.
 
K

Kevin Spencer

You can only have one (ASP.Net) WEBFORM on a page. This doesn't mean that
you can't have more than one HTML form on a page. In fact, you can add as
many HTML forms to the page as you wish.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
W

William F. Robertson, Jr.

Place your member's login in a user control that is placed on the top of
every page. You an handle the "login" button click in the UserControl.

You will only have to code it once.

bill
 
A

Alan Silver

You can only have one visible server form on the page at a time.
So you could have more than one server form as long as you only show one at
a time.
Or you can have more than one form visible on your page at a time, but only
one of them can be a server form (i.e. with the runat=server attribute.)
Server forms only support posting back to themselves (not to other pages.)

Ah, so the search form would be a server form, and the log-in form would
be a standard old HTML form. Clever. I guess without the self-posting
stuff (which you wouldn't have as the log-in form posts to another
page), you don't need the ASP.NET bits.

Thanks, that helps a lot.
This will all become more flexible in .NET version 2.

In what way? just interested as, given that I am learning from scratch,
I am trying to do things the 2.0 way from the start as it will save
relearning later. I reckon by the time I'm ready to roll out a
production ASP.NET site, 2.0 will be out of beta.
 
A

Alan Silver

You can only have one (ASP.Net) WEBFORM on a page. This doesn't mean that
you can't have more than one HTML form on a page. In fact, you can add as
many HTML forms to the page as you wish.

Thanks Kevin, you and Juan both gave the same answer ;-) I'm a lot
clearer now thanks to you two.
 
S

Steve C. Orr [MVP, MCSD]

Well, for starters, in ASP.NET 2.0 you'll be able to have your server forms
post to other pages.
 
A

Alan Silver

Well, for starters, in ASP.NET 2.0 you'll be able to have your server forms
post to other pages.

Oh, clever stuff. I saw a post here earlier that reckoned 2.0 wasn't due
until the end of the year. I hope I'll be doing this properly long
before that!! Looks like I'll be learning 1.1 after all.

Thanks for the reply
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top