简体   繁体   English

ASP.NET VS2008 C#-下拉列表-回发

[英]ASP.NET VS2008 C# - dropdown list - postback

How can use a dropdown list without the autopostback=true. 如何在没有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). 对我来说,下拉菜单需要做一些特别的事情,例如为Javascript添加项目属性等。这是需要在每次页面加载时添加的数据(也就是在下拉菜单中未保留的数据,例如每个项目的名称和值)。 In these cases I have done this work on load, then I try to retrieve the value later in the page lifecycle and DOH! 在这些情况下,我已经在加载时完成了这项工作,然后稍后在页面生命周期和DOH中尝试检索该值!

Here is the page lifecycle: 这是页面生命周期:

http://msdn.microsoft.com/en-us/library/ms178472.aspx 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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