Asp.Net nested form

2019-04-12 08:02发布

问题:

I need to supply an html form (not a server form) for searching within an asp.net web-forms website. The form will post to another website where an indexed search is performed. Seeing as nested forms don't work well in asp.net, what is the simplest approach for this?

The nested form is a simple html form that performs a "get" against the search website, which I do not have programmatic control over.

Update: I resolved my issue by simply moving the server form to the appropriate place on the page, rather than having it surround the entire page. However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).

回答1:

However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).

You can place controls on your page without requiring an HtmlForm.

In your case there's no issue declaring another form markup, but you could also just use some search control on your main form and make it issue a GET to that website.



回答2:

Not only do nested forms "not work well," you basically can't have >1 form per page at all. The simplest approach is the approach you are forced to go with: write a page that only uses one <form runat="server"></form>. Since you need search functionality, is there no ASP.NET search box control that you could use?

Have a read here.



回答3:

There are 4 workarounds:

  1. Use an IFRAME
  2. Force Submission to Navigate Using a GET Request
  3. Dynamically Change the Form Action
  4. Use a 3rd Party Form Handler

More details on http://www.revindex.com/Blogs/tabid/65/EntryID/21/Default.aspx



回答4:

Nested forms don't work well in HTML full stop! You should never do it.

Perhaps you mean more than one form on page? Whilst it's true you can only have one form with runat="server", I can't see any reason why you couldn't have a standard form (not server form) that posted to another site at the same level (ie. not nested).



回答5:

Try adding your HTML input elements to wherever you want the nested form to be. Then use JQuery to change the page form action to point to the external Website. The entire form is submitted, but with a different external Url. The minor downside is the values for all input elements on the page are posted, but most times that is not big deal.

(I only tried this using a POST, not a GET. This is basically Roman O's #3 workaround in more detail)

<div id="nested-form">
    <input type="text" name="q">            
    <input name="searchbtn" value="Go" type="submit" class="search-button">
</div>

<script>
$(function() {
  $("input.search-button").click(function() {      
        $('form').get(0).setAttribute('action', 'http://external.com');
  });
});
</script>


回答6:

maybe you try Server.Transfer() to your target page that do the search from a button for example!