我的 aspx 页面中有 10 个列表框,所有 10 个列表框都使用相同的功能用于某些按钮我想将列表框数据添加到网格你能帮我我的 java 脚本代码如下所示
function MoveItem(ctrlSource, ctrlTarget) {
var Source = document.getElementById(ctrlSource);
var Target = document.getElementById(ctrlTarget);
if ((Source != null) && (Target != null)) {
while ( Source.options.selectedIndex >= 0 ) {
var newOption = new Option(); // Create a new instance of ListItem
newOption.text = Source.options[Source.options.selectedIndex].text;
newOption.value = Source.options[Source.options.selectedIndex].value;
Target.options[Target.length] = newOption; //Append the item in Target
Source.remove(Source.options.selectedIndex); //Remove the item from Source
}
}
我尝试使用上面的 javascript 代码在列表框之间移动项目,当我尝试保存 listbox.items.count 给 0 时出现问题,谁能告诉我为什么会发生这种情况以及回发时列表框项目丢失。
最佳答案
亲爱的,
这是正常的,客户端对列表框所做的任何更改都不会反射(reflect)在服务器端。
您可以在按钮上添加一个 onclientclick 事件并从列表框客户端读取项目并使用 AJAX 保存它们,或者您可以从列表框客户端读取项目并将它们作为字符串放在隐藏字段中,您可以读取隐藏字段服务器端的值。
您可以从客户端读取所有项目,并将它们格式化为您自己的格式作为字符串。
然后在页面上添加一个asp隐藏字段,客户端将你的字符串放在隐藏字段值属性中。
现在您可以从服务器端的 onclick 事件访问字符串并提取列表框的项目。
关于c# - 回发发生在 asp.net 中丢失的 javascript 列表框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4614826/