简体   繁体   English

使用javascript将值从一个列表框移动到另一个列表框然后使用c#读取值

[英]move value from one listbox to other using javascript and then read value using c#

i have two listbox(listbox 1 and listbox2).i have used following javscript code to move value from one listbox to other. 我有两个列表框(列表框1和列表框2).i使用以下javscript代码将值从一个列表框移动到另一个列表框。

 <script language="javascript" type="text/javascript">

function fnMoveItems(lstbxFrom,lstbxTo)
{
 var varFromBox = document.all(lstbxFrom);
 var varToBox = document.all(lstbxTo); 
 if ((varFromBox != null) && (varToBox != null)) 
 { 
  if(varFromBox.length < 1) 
  {
   alert('There are no items in the source ListBox');
   return false;
  }
  if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1

  {
   alert('Please select an Item to move');
   return false;
  }
  while ( varFromBox.options.selectedIndex >= 0 ) 
  { 
   var newOption = new Option(); // Create a new instance of ListItem 

   newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text; 
   newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value; 
   varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox

   varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox 

  } 
 }
 return false; 
}
</script>

This code moves value from one listbox to another,but actually when i try to read the second listbox values, one to whhich values are copied , i am not able to read those values. 此代码将值从一个列表框移动到另一个列表框,但实际上当我尝试读取第二个列表框值时,其中一个值被复制,我无法读取这些值。 when i check it shows ListBox2.Items.Count is 0 当我检查它显示ListBox2.Items.Count是0

As Amar Palsapure stated in the comments, changes on clientside with javascript does not reflect on server side without some hacking on your part (add the values to hidden fields etc. have a look here ), so you would not be able to see the changes server side. 正如Amar Palsapure在评论中指出的那样,客户端使用javascript的更改并不反映在服务器端而没有您的一些黑客攻击(将值添加到隐藏字段等等,请查看此处 ),因此您将无法看到更改服务器端。 I assume the line ListBox2.Items.Count is server side. 我假设行ListBox2.Items.Count是服务器端。

It would be a lot better and easier for you if you do an ajax request and do it server side within an update panel. 如果您执行ajax请求并在更新面板中执行服务器端,那么对您来说会更好,更容易。

确保在页面加载时不要将列表框设置代码放在if语句中,以确保它不是回发。

The server side process (C) is not able to read from the client, without an HTTP request - page reload(which is probably not what you want to do). 没有HTTP请求的服务器端进程(C)无法从客户端读取 - 页面重新加载(这可能不是您想要做的)。 Your javascript looks good, but you probably need to use AJAX technique, which allows your client code to speak to your server code without reloading the page in the traditional HTTP request model. 您的javascript看起来不错,但您可能需要使用AJAX技术,它允许您的客户端代码与您的服务器代码通信,而无需在传统的HTTP请求模型中重新加载页面。

Try using JQuery library to assist in setting up your request to the server. 尝试使用JQuery库来帮助设置对服务器的请求。 http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

As TBohnen.jnr suggested I recommend using an Update Panel and then an Asynchrounous Postback Trigger to refresh the update panel. 正如TBohnen.jnr建议我建议使用更新面板,然后使用Asynchrounous Postback Trigger刷新更新面板。 You will need to put the list boxes inside the panel and then have an event that you raise when the content is moved. 您需要将列表框放在面板中,然后在移动内容时生成一个事件。

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

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