posting a form but without posting the viewstate

放肆的年华 提交于 2019-12-11 01:55:14

问题


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.


回答1:


You have a couple of options here:

  • Disable ViewState
  • Use jQuery to remove the fields you don't want to post before the are sent to the server

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.




回答2:


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.




回答3:


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:


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" />


来源:https://stackoverflow.com/questions/2903033/posting-a-form-but-without-posting-the-viewstate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!