我的 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/