views:

1970

answers:

5

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 bookmarkeable URLs, plus it feels cleaner to do it this way.
I obviously don't want all the viewstate crap hidden fields appended to the URL either.

The best I could come up with for this is:
a) either capture the server-side click event of the button and Response.Redirect
b) Attach a Javascript onclick handler to the button, and to window.location.replace

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


I'm guessing this is one solid answer to my previous question: Why does the ASp.Net Web Forms model suck?

+2  A: 

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.

seanb
Yep, but as you state, my masterpage does wrap everything around with the main server form (since every single other page is a regular .net webform)Thanks for the answer, though!
Daniel Magliola
I have worked around that a few times by adding another placeholder outside the form, but inside the main content div. Depends on how your app is laid out, but is sometimes possible, if your nav relies on being inside the form, then probby not possible.
seanb
FYI - The "wrapper" form is only created if you do not create your own form elements. I have master pages with HTML and server-side forms in master pages all over the place :)
Rob Cooper
I would say "use a HTML form" as well though, so +1 for that part of the answer :)
Rob Cooper
A: 

I would do (b) since (a) would require two round trips for a single query. Alternatively, you could disable viewstate on the page, remove any other hidden fields via javascript, and also use javascript to modify the form method from post to get. I've never done this for real, but my toy page using the included sample worked like a charm. It's arguably easier than encoding the search string and doing the get via javascript.

Actually, it sounds like you would be happier with ASP.NET MVC since this is easily doable there by simply setting the form method to GET in the view.

sample code using jquery

 $(document).ready( function() {
     $('input[type=hidden]').remove();
     $('form').attr('method','get');
 });

EDIT: It seems like you ought to be able to do the same thing server-side, too. Maybe in OnPreRenderComplete. Don't have access to Visual Studio right now to check.

tvanfosson
A: 

I have always used Response.Redirect as it "works".

I don't think there is an optimal method.

cbp
+1  A: 

I worked on a web site that had to post to a 3rd party site to do the search on the client's web site. I ended up doing a simple Response.Redirect and passed in the search parameters through the query string like so:

protected void Button1_Click(object sender, EventArgs e)
{
    string SearchQueryStringParameters = @"?SearchParameters=";
    string SearchURL = "Search.aspx" + SearchQueryStringParameters;

    Response.Redirect(SearchURL);
}

And on your Search.aspx page in your pageload...

protected void Page_Load(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(Request.QueryString["SearchParameters"]))
    {
        // prefill your search textbox
        this.txtSearch.Text = Request.QueryString["SearchParameters"];

        // run your code that does a search and fill your repeater/datagrid/whatever here
    }
    else
    {
        // do nothing but show the search page
    }
}

Hope this helps.

Solburn