Using GET in ASP.Net Forms

By default, ASP.Net Forms use the POST method to handle all callbacks to the form (button and linkbuttons clicks, control changes, etc.)

Query forms, however, usually, tend to use the GET method, following the logic: use GET for queries, use POST for changes.

Also, if you use GET, the search term will become part of the URL, which lets you bookmark a search result.

How to setup a form for GET? Simply set the Form.Method:

this.Form.Method = "GET";

However you will find that ASP.Net adds several parameters to the query URL, depending on the form you design, such as __VIEWSTATE and __EVENTVALIDATION.

Therefore, we have to disable ViewState (usually the only thing we want from the query form is the search string, so we don’t lose functionality here).

However, the __VIEWSTATE hidden variable is still generated, so one line of Javascript is needed to clear it.

protected void Page_Load(object sender, EventArgs e)
  this.Form.Method = "GET";
  this.Form.EnableViewState = false;
  this.ClientScript.RegisterClientScriptBlock(GetType(), "clr",
document.getElementById('__VIEWSTATE').value = '';
", true);

  Label1.Text = q.Text;    // output query string

In Google fashion, the example uses TextBox named q to contain the search term😉

2 Responses to Using GET in ASP.Net Forms

  1. […] GET and __VIEWSTATE in ASP.Net Forms In my previous post, I described the steps to set up a form to use the HTTP GET […]

  2. Hello,
    I need help with browser back button. It throws (in IE) “webpage has expired’. I’ve tried many workarounds proposed: removed metatags, removed no-cache from pages..but nothing helps. After postback and browser back, it throws the above about Post Redirect Get pattern but, as we have a really big project, rewrite all pages business to redirect after each postback is a suicide and my team lead doesn’t accept this solution. Can you please give some ideas how to fix it ? Thank you🙂.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: