Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple forms on one ASP.NET 2.0 page

Status
Not open for further replies.

ShonGill

Programmer
May 20, 2008
44
US
Hello Everyone,

Can anyone suggest a way to use multiple forms that are completely seperate from each other on a single ASP.NET 2.0 page? What I have is a simple search form that is constructed of HTML submits with the method="GET".

I need to implement this form on every page, including a contact page which of course has validation and sends emails through a code behind.

I've seen several ways in just searching online including: using ASP Panels with individual ID's and also setting form attributes to the submit button. The Button1.Attributes.Add seems like it's working. However, the resource is a remote site and the form is not passing the necessary hidden input fields.

Is there a better way to do this?



 
if you are using webforms in the postback model then you can only have 1 <asp:form runat="server"> per page.
you can have as many html forms as you like. however these will not work with the postback model (at least i'm 99% sure about that)

i wouldn't recommend mixing the 2. either use html forms with an MVC approach, or use the webform postback model. with the postback model I would create a web user control and place this on the master page, within the form tags.
Code:
//header
<asp:form id="form1" runat="server">
   <asp:MySearchControl id="seacher" runat="server"/>
   <asp:ContentPlaceHolder id="content" runat="server"/>
</asp:form>
//footer

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
As Jason points out, you can only have one form tag with runat"server" in it. However, you can have as many forms on the page as you like as long as they don't have this attribute and they are also not allowed to be nested inside one another.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
So waht about using ASP Panels? Don't they seperate one form inside another form? I'm not using master pages. In the past I used a javascript to turn off the page's postback and then I could run the HTML form. No w I have a form that is supposed to work without the method="GET/POST". Is there a way to use javascripting on my page to accomplish this very simple search?

Also, Mark, I'm referring someone over to you.



 
Disregard. I think I see what you mean now. I'll look into it further. Thanks guys. : )


 
If my post wasn't very clear, I meant you can do this:
Code:
<form id="form1" runat="server">
...
</form>
<form id="form2" method="post" action="[URL unfurl="true"]http://whatever.com">[/URL]
...
</form>
<form id="form3" method="post" action="[URL unfurl="true"]http://whatever2.com">[/URL]
...
</form>


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
I tried setting it up that way, but I think when I tried it I had the forms nested. I was using something like this:

Code:
<body>
   <form id="form1" runat="Server">
      
      <asp:panel id="panel2" runat="server">
          
         <form id="form2" method="get" action="[URL unfurl="true"]HTTP://WEBSITE.COM">[/URL]
         </form>
      
      </asp:panel>
      <asp:panel id="panel3" runat="server">
         <asp:textbox id="textbox1" runat="server"></asp:textbox>
         <asp:button id="Button1" runat="server" />
      </asp:panel>
   
   </form>
</body>
 
Is it surprising that it gave me PostBack errors, not form nested in another form or anything like that?
 
Yeah, it is PostBack/Validation... basicly "Your did something wrong" error messages. At least that's the impression that I get and your right the error is not specific. So, is there any problem with using the runat="server" form in the middle of the page?

I'm not trying to just dump all my content on one page. I got the impression the HTML for my layout had to be inside the form tags. I knew it must have been an "OE" issue. LOL : )

Thanks again.



 
The form with the runat="server" tag should just be around the server-side control so if that is the "middle" of your page then yes that's fine. Have a look at the "view source" of my aspnetlibrary site which includes two forms; one for a google search and one for the server controls.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
I was looking at it earlier, and it looks like you have hidden inputs with VIESTATE. That is where I got lost. I was expecting to see an ASP.NET form that inherently gets what it needs to postback, and here on your site it looks like your manually loading the viewstate and creating a postback.

Very cool in thought, but more than what I know how to do as of yet. Ultimately I'm trying to accopmplish the same. A google search at the top of page and I need to keep the postback functionality throughout the rest. I think I have an idea now, how to accomplish this. I'm going to try a few things when I'm in front of it.

Is there anything I may run into and should look out for?


 
ShonWeb, I think you are starting to get confused between http get/post and the asp.net life cycle. the asp.net life cycle is a wrapper around http get/post. no more, no less. some love it, some hate it. life cycle events are a convience for developers.

when you use a webform to generate an html page the webform must add all kinds of hidden values/js to make it work. viewstate is one of them. an <asp:form/> is just an html form that asp.net is aware of.

your attempting to mix the asp.net life cycle with standard html. While it can be done, it's like mixing water and oil.



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I am confused. I remember the method="Get/Post" and how to wuse it from a few years back. before CDO was used over CDONTS (I never liked CDONTS anyway).

The funny thing is, I know what I'm confused about and have a clear idea what is making me confused. Like you say, I'm trying to mix oil and water. I'm not the only who needs this functionality though. Perhaps it may all get combined into the newer greater thing and we'll just call it Oi-ter.

: )


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top