posting a form but without posting the viewstate

2019-07-21 17:35发布

I have a web form and want to 'get' it to another page.. is there anyway to submit it without posting the ViewState and other bits I don't want?

Or should I be catching the submit button click and redirecting with a querystring I build myself.

4条回答
贼婆χ
2楼-- · 2019-07-21 17:41

If you are not using the viewstate, why have you kept it enabled? Just disable it. For every server control, set the EnableViewState = False and you are free from it. If you need the viewstate, it will be part of the post all the time.

查看更多
Rolldiameter
3楼-- · 2019-07-21 17:42

There are different ways to persist viewstate.

I have had in the past, had to persist viewstate on the server (using ApplicationState/Session, cant remember) for a heavy AJAX page to support faster updates. Works well.

See Page.LoadPageStateFromPersistenceMedium and Page.SavePageStateToPersistenceMedium.

Sorry, no links from Reflector available.

查看更多
做个烂人
4楼-- · 2019-07-21 17:52

You could add an event handler to your search button and do something similar to this

private void button_Click(object sender, EventArgs e)
{
    String query = queryTextBox.Text;
    Response.Redirect("SearchResults.aspx?query=" + query);
}

Using JavaScript

function doSearch()
{
    // Assuming you are not using jQuery, 
    // using jQuery it would be $('#queryTextBox').value instead
    var queryString = document.getElementById('queryTextBox').value;

    window.open("SearchResults.aspx?query=" + queryString);
    return false;
}

Html

<input type="text" id="queryTextBox" />
<input type="button" onclick="return doSearch()" value="Go" />
查看更多
该账号已被封号
5楼-- · 2019-07-21 18:05

You have a couple of options here:

You don't have to disable ViewState on all pages, just the pages that you do not care for the state to be saved.

But there is also the option to disable the ViewState completely if you never want to use it.

If you just want to compose a GET by yourself, you can use jQuery for that aswell so you only pass the parameters you really want which will give you 100% control of what is posted /getted.

查看更多
登录 后发表回答