3

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

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Right, I didn't actually mean an "HtmlForm" server control. I had used a form tag with the get method and upon clicking it's contained button, the entire page posted back rather than going to the "action" page of that form. It sounds like you are saying that should work? Perhaps I overlooked something else if so.
What I ment was exactelly what you have already done (rendering another form just outside your main HtmlForm) and that's perfectly fine. The other solution I was sugesting is to use some custom search control (textbox & button) and embed the logic to just issue an HTTP GET with it.
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.

1 Comment

Righto - "can't have >1 form per page at all" meaning forms with runat="server". You can have other forms on the same page as long as they don't have runat="server". Sorry if that was less-than-clear.
2

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

Comments

1

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

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.