javascript - 使用 JavaScript 设置的值不会保留在回发中

标签 javascript asp.net postback

我的 asp.net 页面中有两个列表控件,我正在使用 javascript 填充第二个列表控件。问题是脚本执行,我可以看到值从第一个列表框 (ConfiguredOrgListBox) 移动到第二个列表框 (SelectedOrgListBox) 但是当我尝试使用提交按钮保存时,我发现我的第二个列表为空,第一个列表框和之前一样.下面是脚本和标记。

    //call this method to register the script    
    private void CreateMoveOrganizationScript(StringBuilder sb) {
        sb.Append( @"<script language=javascript type=text/javascript>;
                    function moveOrganisation() {");            
        sb.Append( @"var source = document.getElementById('"+ ConfiguredOrgListBox.ClientID  +@"');
                    var target = document.getElementById('"+SelectedOrgListBox.ClientID+ @"');
                    if ((source != null) && (target != null)) {
                    var newOption = new Option();
                    newOption.text = source.options[source.options.selectedIndex].text;
                    newOption.value = source.options[source.options.selectedIndex].value;

                    target.options[target.length] = newOption;
                    source.remove(source.options.selectedIndex)  ;
                    }            
                } </script>");            
    }

标记

      <asp:Label ID="ConfiguredOrgLabel" runat="server" Text="Available Organizations"></asp:Label><br />
      <asp:ListBox ID="ConfiguredOrgListBox" runat="server" Width="98%" Height="100px"></asp:ListBox>
       <input id="MoveOrgRight" type="button" value=">>" onclick="moveOrganisation()" />
      <asp:Label ID="SelectedOrgLabel" runat="server" Text="Selected VNA Organizations"></asp:Label><br />
      <asp:ListBox ID="SelectedOrgListBox" runat="server" Width="98%" Height="100px"></asp:ListBox>

请告诉我我做错了什么

问候, 杰兹

最佳答案

根据 this ,这是因为列表框没有回发告诉后端它已经改变了。他们使用一个隐藏字段来保存有关使用 JavaScript 进行了哪些更改的信息,然后在回发时更新后端。

关于javascript - 使用 JavaScript 设置的值不会保留在回发中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4089425/

相关文章:

javascript - CSS半固定元素?

javascript - 如何在 Opera 中设置样式显示?

sql - azure 的云。每个(asp.net 注册)客户端一个数据库

asp.net - 使用 ASP.NET MVC 3 构建的 REST API 的版本控制 - 最佳实践

c# - 最初禁用的按钮在通过 Javascript 启用后不会回发

javascript - 我有一个 header.jsp,它的样式会随着不同的登录而不断变化,它是一个动态标题

javascript - 如何使用 JQuery .done()

c# - Asp.NET MVC 应用程序上的 MySQLRoleProvider 错误但仅使用 SQLServer

Javascript:强制aspx父页面服务器刷新

c# - 单击按钮后回发后如何显示 div?