简体   繁体   中英

ASP.NET VS2008 C# - dropdown list - postback

How can use a dropdown list without the autopostback=true.

The value on the server is not being changed according to the one selected from the client side. As I already stated I do not wish that for each dropdown I have the autopostback will trigger a post back.

它将保存在viewstate中,因此,当您最终回发时,该值将是正确的;如果您真的很想获得没有回发的当前值,则可以使用javascript。

Any time I have lost the value of the drop-down it is because I messed up and repopulated the drop down before handling the value change. For me, it has been drop-downs that I need to do something special with like add item attributes for Javascript, etc. This is data that needs to be added on every page load (aka data that is not persisted in the drop down like the names and values of each item). In these cases I have done this work on load, then I try to retrieve the value later in the page lifecycle and DOH!

Here is the page lifecycle:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Dollars to donuts that is what is happening. You are probably just reloading the items before you get to handling whatever postback event you are using to grab the value. If you are doing this and cannot get around this work flow, just save the selected index at the beginning of the logic that populates the drop-down, then set the selected index of the drop down with that value when done.

最坏的情况是,您可以立即从请求对象中获取值:

string selectedID = Request[DropdownControl.UniqueID];

You should make sure you are only filling the select box with options during the initial page load, and not again during the postback

if (!this.Page.IsPostBack) {
  //fill select box here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM