我正在使用aspx页面,并且必须从数据库向Javascript对话框中填充一个列表。我的主要目的是获取在对话框中选择的值并将其粘贴到文本框中。因此,我按照以下方式填写了列表,并且能够看到这些值;
string mycommand = "SELECT [id], [username] FROM [users]";
SqlDataAdapter x = new SqlDataAdapter(mycommand, ConnectionString);
DataSet ds = new DataSet();
x.Fill(ds, "users");
Userlist.DataSource = ds;
Userlist.DataTextField = "username";
Userlist.DataValueField = "username";
Userlist.DataBind();
我的首页就像;
<div id="UserManagmenetDialogFor"></div>
<div id="UserManagmenetDialog" title="Manage Token Users" style="visibility: hidden; display: none;">
<div>
<div>
Users:
</div>
<select id="Userlist" name="cars" runat="server" multiple="true" style="width: 270px; height: 200px;">
</select>
</div>
<div>
<hr />
<input type="button" value="Cancel" onclick="CancelUserDialog();" />
<input type="button" value="Add" onclick="addUser();" />
<input type="button" value="Remove" onclick="removeUser();" />
</div>
</div>
我的JavaScript函数如下;
function addUser() {
var Userlist = document.getElementById('Userlist').options;
var str = new Array();
for (a = 0; a < Userlist.length; a++) {
str.push(Userlist[a].text);
}
$("#<%=TextBox_TokenUsers.ClientID%>").val(str.join(";"));
$("#UserManagmenetDialog").dialog("close");
}
function ShowAddUserDialog() {
var str = $("#<%=TextBox_TokenUsers.ClientID%>").val();
$("#Userlist").html("");
if (str.indexOf(";") != -1) {
var userarray = new Array();
userarray = str.split(";");
for (a in userarray) {
$("#Userlist").append(new Option(userarray[a], "value"));
}
}
else if (str != "") {
$("#Userlist").append(new Option(str, "value"));
}
$("#UserManagmenetDialog").css("visibility", "visible");
$("#UserManagmenetDialog").css("display", "block");
$("#UserManagmenetDialog").dialog({
appendTo: "#UserManagmenetDialogFor",
height: 380,
width: 300,
draggable: true,
resizable: true,
show: {
effect: "fade",
duration: 300
},
hide: {
effect: "fade",
duration: 500
}
});
}
调用ShowAddUserDialog()的文本框和按钮如下所示;
<div style="clear:both;">
<div class="labelitem" style="width: 150px">
Token Users:
</div>
<asp:TextBox Enabled="true" ID="TextBox_TokenUsers" runat="server"></asp:TextBox>
<span onclick="ShowAddUserDialog();" class="MaxButton" style="text-decoration: none; display: inline-block; border-color: White; min-height: 15px; color: white; font-weight: lighter; text-align: left; padding-top: 1px; padding-bottom: 1px; padding-left: 5px; padding-right: 5px; white-space: nowrap;">
<img src="../images/icons/addbig.png" alt="Add New User" border="0" style="height: 20px; vertical-align: middle;" /><b> Add </b>
</span>
</div>
选择值后,当我单击“添加”按钮时,没有任何效果。我在添加电子邮件时使用了相同的功能,而在js对话框中的文本框填充列表时只有一个区别。
有人可以帮我吗?
致谢