Connecting Tech Pros Worldwide Help | Site Map

2 Forms to make 1 form work - ASP.NET

  #1  
Old June 28th, 2009, 05:12 AM
Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84
I never had this kind of issue before and it is completely surprising. I have a usercontrol where I need 2 forms to make 1 form. When I have only 1 form it submits the page to itself. I have spent hours on this and couldn't find the solutions, thats why I have to come with a hack. I have two forms, I made the first form invisible and let the second form visible.This form is for google custom search engine. I also tried with other test forms, but still I need to have 2 forms to make 1 form work in this user control. Name of the user control is header.ascx (in case the name got anything to do with this weird issue).

Expand|Select|Wrap|Line Numbers
  1. <span style="visibility:hidden;position:absolute;left:0px;">
  2. <form id="cse-search-box" action="search.aspx" onsubmit="return validateSearch();" visible="false">
  3. <div class="searchSpan">
  4. <input name="cx" type="hidden" value="002116985177671925771:cz5fh8mpenw" />
  5. <input name="cof" type="hidden" value="FORID:11" />
  6. <input name="ie" type="hidden" value="UTF-8" />
  7. <input id="q" name="q" size="22" value="test"/>
  8. <input name="sa" id="searchSubmit" type="submit" value="Search" />
  9. <script src="http://www.google.com/jsapi" type="text/javascript"></script>
  10. <script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script>
  11. <script src="http://www.google.com/coop/cse/t13n?form=cse-search-box&amp;t13n_langs=en" type="text/javascript"></script>
  12. <script src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script>
  13. </div>
  14. </form></span>
  15.  
  16. <form id="cse-search-box" action="search.aspx" onsubmit="return validateSearch();">
  17. <div class="searchSpan">
  18. <input name="cx" type="hidden" value="002116985177671925771:cz5fh8mpenw" />
  19. <input name="cof" type="hidden" value="FORID:11" />
  20. <input name="ie" type="hidden" value="UTF-8" />
  21. <input id="Text1" name="q" size="22"/>
  22. <input name="sa" id="Submit1" type="submit" value="Search" />
  23. <script src="http://www.google.com/jsapi" type="text/javascript"></script>
  24.  <script type="text/javascript">google.load("elements", "1", { packages: "transliteration" });</script>
  25. <script src="http://www.google.com/coop/cse/t13n?form=cse-search-box&amp;t13n_langs=en" type="text/javascript"></script>
  26. <script src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script>
  27. </div>
  28. </form>
  29.  
But this kind of issue doesn't happen in another user control where I have different form and it works normally. Anyone had this kind of issue before??
  #2  
Old June 29th, 2009, 05:28 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

re: 2 Forms to make 1 form work - ASP.NET


I have no idea what you're trying to explain.

Please note that if you set an ASP.NET control's Visible property to False it will not be rendered in the browser...

I have feeling that you are going to want to look into using Web Services with AJAX.NET to make an asynchronous call to the searching service that returns you some results.

I don't know why you would ever need 2 forms.
Never mind that but the <form> tag is meant to be used in the ASPX page, not an ASCX (user control)....

What are your requirements?

I think you're going to need to take a look at the search service that you're using....if you could provide us with a link to the API for this search service it might help us give you a better answer.

-Frinny
  #3  
Old June 30th, 2009, 02:13 PM
Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84

re: 2 Forms to make 1 form work - ASP.NET


Thanks for the answer. Actually the problem was that I have a page that is using different controls and those user controls are within a single form tag. And in the header user control (where the search bar is), I have another form tag, which wasnt working at all, thats why I used that "hack" of two forms to make that search form work. After some research I found out that I cannot have a form tag within another form tag. My main page looks like this

Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <form id="myForm" action="">
  3. <table id="mainTable" align="center" border="0">
  4.     <tbody>
  5.     <tr>
  6.         <td>
  7.         <uc:pageHeader ID="pageHeader" runat="server" />
  8.  
  9.         <uc:topMenu id="topMenu" runat="server" />
  10.         <table id="mainContainer" border="0" cellspacing="0px">
  11.             <tbody>
  12.             <tr>
  13.                 <!-- Left Menu -->
  14.                 <uc:leftMenu ID="leftMenu" runat="server" />
  15.  
  16.                 <td id="mainContainerTextArea" valign="top">
  17.                     <uc:homeText id="homeText" runat="server" />
  18.  
  19.                 </td>
  20.             </tr>
  21.         </tbody>
  22.         </table>
  23.  
  24.         </td>
  25.     </tr>
  26.     <uc:footer ID="footer" runat="server" />
  27. </tbody>
  28. </table>
  29. </form>
  30. </body>
  31. </html>
  32.  
Btw, do I Really need user controls wrapped in form tags. User controls are working fine without being wrapped in form tags as well.
  #4  
Old June 30th, 2009, 02:20 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

re: 2 Forms to make 1 form work - ASP.NET


Your user controls should never have <form> tags in them.
User controls are meant to be added to ASPX pages which have a <form> tag...even the ASPX pages might not have a <form> tag in them depending on whether or not you are using MasterPages.


You should only ever have 1 <form></form> tag on the page (whether it be in the ASPX page or in the MasterPage).
  #5  
Old June 30th, 2009, 02:39 PM
Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84

re: 2 Forms to make 1 form work - ASP.NET


Quote:
Originally Posted by Frinavale View Post
Your user controls should never have <form> tags in them.
User controls are meant to be added to ASPX pages which have a <form> tag...even the ASPX pages might not have a <form> tag in them depending on whether or not you are using MasterPages.


You should only ever have 1 <form></form> tag on the page (whether it be in the ASPX page or in the MasterPage).
What if I have two different portions of a page and both submit themselves to a different page, then one form tag won't work. Don't I need two form tags then?

Also, whats the reason that we shouldn't have form tag(s) in user controls? Any security or performance risk?

Thanks for the reply :)
  #6  
Old June 30th, 2009, 03:07 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

re: 2 Forms to make 1 form work - ASP.NET


Quote:
Originally Posted by programmerboy View Post
What if I have two different portions of a page and both submit themselves to a different page, then one form tag won't work. Don't I need two form tags then?
It's possible but it makes things confusing.

There is a way to have elements on one page submit to another... but really why would you need to? If you must do this then your design is probably unnecessarily complicated.

Quote:
Originally Posted by programmerboy View Post
Also, whats the reason that we shouldn't have form tag(s) in user controls? Any security or performance risk?
It has nothing to do with security, nor performance. The reason you should not have a form tag in a user control is because user controls are supposed to be used (as a control) in a page. As I stated earlier, the page that the user control is added to will have the form tag, and (as you've discovered) you cannot have a form tag within a form tag.


Please note that User Controls have a page life cycle that is the same as an ASPX page. So anything submitted to the User Control will seem like it's "submitting to it's own page"....in other words, you implement a User Control the same way that you would implement an ASPX page, but the User Control does not have a form tag. That belongs to the parent page of the User Control.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to make nonrectangular forms work with 32 bit color Ken Allen answers 2 November 20th, 2005 03:25 AM
Hard Question - How to make a TextBox always write in English Gidi answers 14 November 17th, 2005 05:38 AM
How to make a comboBox behave like a Label at runntime malcolm answers 5 November 16th, 2005 12:36 PM
C# : How to make connection avilable to all forms & classes in an Application? RD answers 2 November 15th, 2005 06:52 PM
How to make all access to linked table read-only Bob Alston answers 24 November 13th, 2005 01:39 PM