Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do <form method="get"> in ASP.Net for a search form?

I have a search form in an app I'm currently developing, and I would like for it to be the equivalent of method="GET".

Thus, when clicking the search button, the user goes to search.aspx?q=the+query+he+entered

The reason I want this is simply bookmarkable URLs, plus it feels cleaner to do it this way.

I also don't want the viewstate hidden field value appended to the URL either.

The best I could come up with for this is:

  1. Capture the server-side click event of the button and Response.Redirect.
  2. Attach a Javascript onclick handler to the button that fires a window.location.replace.

Both feel quirky and sub-optimal... Can you think of a better approach?

like image 662
Daniel Magliola Avatar asked Nov 26 '08 01:11

Daniel Magliola


People also ask

Can I submit form with GET method?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ).

How do you get information from a form that is submitted using the GET method?

The Correct Answer is " Request.

What does get method do in HTML form?

The GET method is the method used by the browser to ask the server to send back a given resource: "Hey server, I want to get this resource." In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method the data sent to the server is appended to the URL.


1 Answers

Use a plain old html form, not a server side form (runat=server), and you should indeed be able to make it work.

This could however be a problem if you have an out of the box visual studio master page which wraps the entire page in a server side form, because you can't nest forms.

Web forms don't have to suck, but the default implementations often do. You don't have to use web forms for everything. Sometimes plain old post/get and process request code will do just fine.

like image 114
seanb Avatar answered Oct 23 '22 20:10

seanb