每次单击按钮(btnadd)时页面都会后备。可以任何人告诉我如何存储列表框的选定值。这是从一个列表框添加到另一个列表框。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBoxExample.aspx.cs" Inherits="ListBoxExample"
EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Adding,removing elements from First Listbox to Second Listbox</title>
<style type="text/css">
.lstbx1
{
font-family: Verdana;
font-size: medium;
font-style: normal;
background-color: Aqua;
height: auto;
width: auto;
}
.lstbx2
{
font-family: Verdana;
font-size: medium;
font-style: normal;
background-color: Lime;
height: auto;
width: auto;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
$('#btnadd').click(function () {
// var originalList = $("#<%= this.lstbx1.ClientID %>");
var items = $('option', originalList);
var targetList = $("#<%= this.lstbx2.ClientID %>");
$("#lstbx1 option:selected").remove().appendTo("#lstbx2");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:ListBox ID="lstbx1" runat="server" CssClass="lstbx1" SelectionMode="Multiple">
<asp:ListItem>John</asp:ListItem>
<asp:ListItem>Peter</asp:ListItem>
<asp:ListItem>Sam</asp:ListItem>
</asp:ListBox>
</td>
<td>
<asp:ListBox ID="lstbx2" runat="server" CssClass="lstbx2"></asp:ListBox>
</td>
</tr>
<tr>
<td>
<input type="button" id="btnadd" value="Add" />
</td>
<td>
<asp:Button ID="btnremove" runat="server" Text="Remove" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
将您在jquery中所做的更改保存到隐藏字段值f.e.以逗号分隔的值列表进行更改,在服务器端拆分值并将其添加到列表框中。否则,在回发后您的更改不会保留。