2 Forms to make 1 form work - ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • programmerboy
    New Member
    • Jul 2007
    • 84

    2 Forms to make 1 form work - ASP.NET

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

    Code:
    <span style="visibility:hidden;position:absolute;left:0px;">
    <form id="cse-search-box" action="search.aspx" onsubmit="return validateSearch();" visible="false">
    <div class="searchSpan">
    <input name="cx" type="hidden" value="002116985177671925771:cz5fh8mpenw" />
    <input name="cof" type="hidden" value="FORID:11" />
    <input name="ie" type="hidden" value="UTF-8" />
    <input id="q" name="q" size="22" value="test"/>
    <input name="sa" id="searchSubmit" type="submit" value="Search" />
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script>
    <script src="http://www.google.com/coop/cse/t13n?form=cse-search-box&amp;t13n_langs=en" type="text/javascript"></script>
    <script src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script>
    </div>
    </form></span>
    
    <form id="cse-search-box" action="search.aspx" onsubmit="return validateSearch();">
    <div class="searchSpan">
    <input name="cx" type="hidden" value="002116985177671925771:cz5fh8mpenw" />
    <input name="cof" type="hidden" value="FORID:11" />
    <input name="ie" type="hidden" value="UTF-8" />
    <input id="Text1" name="q" size="22"/>
    <input name="sa" id="Submit1" type="submit" value="Search" />
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
     <script type="text/javascript">google.load("elements", "1", { packages: "transliteration" });</script>
    <script src="http://www.google.com/coop/cse/t13n?form=cse-search-box&amp;t13n_langs=en" type="text/javascript"></script>
    <script src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script>
    </div>
    </form>
    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??
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • programmerboy
      New Member
      • Jul 2007
      • 84

      #3
      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

      Code:
      <body>
      <form id="myForm" action="">
      <table id="mainTable" align="center" border="0">
      	<tbody>
      	<tr>
      		<td>
      		<uc:pageHeader ID="pageHeader" runat="server" />
      		
      		<uc:topMenu id="topMenu" runat="server" />
      		<table id="mainContainer" border="0" cellspacing="0px">
      			<tbody>
      			<tr>
      				<!-- Left Menu -->
      				<uc:leftMenu ID="leftMenu" runat="server" />
      
      				<td id="mainContainerTextArea" valign="top">
      				    <uc:homeText id="homeText" runat="server" />
      				
      				</td>
      			</tr>
      		</tbody>
      		</table>
      		
      		</td>
      	</tr>
      	<uc:footer ID="footer" runat="server" />
      </tbody>
      </table>
      </form>
      </body>
      </html>
      Btw, do I Really need user controls wrapped in form tags. User controls are working fine without being wrapped in form tags as well.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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).

        Comment

        • programmerboy
          New Member
          • Jul 2007
          • 84

          #5
          Originally posted by Frinavale
          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 :)

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by programmerboy
            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.

            Originally posted by programmerboy
            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.

            Comment

            Working...