大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索
打印 上一主题 下一主题

Asp.net listbox javascript 无需 postback

[复制链接]
跳转到指定楼层
1#
发表于 2009-6-1 08:38:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
来源:http://www.dotnet-developer.de/2008/07/aspnet2005/ajax/using-javascript-on-listboxes-in-aspnet/

The JavaScript code:
<script type="text/javascript" language="javascript">
  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 HiddenList = document.getElementById('<%= SelectedAdditionalInvolved.ClientID %>') //The hidden field
          var SelectedValue = Source.options[Source.options.selectedIndex].value + ','; // Hidden List is comma seperated
          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
          if (HiddenList.value.indexOf(SelectedValue) == -1)
          {
            HiddenList.value += SelectedValue; // Add it to hidden list
          }
          else
          {
            HiddenList.value = HiddenList.value.replace(SelectedValue,""); // Remove from Hidden List
          }
      }
    }
   }
</script>
In my aspx:
<span class ="selectionListUsernames">
  <aspistBox ID="AdditionalUsers" Width="100%" Height="100%" runat="server" AutoPostBack="false" />
</span>
<span class="selectionListUsernamesButtons"><br /><br />
  <input onclick="Javascript:MoveItem('<%= AdditionalUsers.ClientID %>', '<%= AdditionalUserSelection.ClientID %>');" type="button" value="->" /><br /><br />
  <input onclick="Javascript:MoveItem('<%= AdditionalUserSelection.ClientID %>', '<%= AdditionalUsers.ClientID %>');" type="button" value="<-" />
</span>
<span class="selectionListUsernames">
  <aspistBox ID="AdditionalUserSelection" width="100%" Height="100%" runat="server" AutoPostBack="false" />
</span><asp:HiddenField ID="SelectedAdditionalInvolved" runat="server" />  


In my css:
.selectionListUsernames
{
    float:left;
    width: 300px;
    height: 150px;
}

.selectionListUsernamesButtons
{
    float: left;
    width: 120px;
    text-align: center;
    vertical-align:middle;
}
SelectedAdditionalInvolved 全部数值都会存在里头

手机版|大马资讯论坛  

GMT+8, 2024-11-1 06:51 , Processed in 0.019215 second(s), 12 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表