Asp.Net nested form

限于喜欢 提交于 2019-12-04 12:28:29

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.

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.

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

Dan Diplo

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

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>
0xFF

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!