我正在使用jQuery multiselect,因为我正在使用按钮点击事件,该事件进入服务器端并填充另一个下拉列表中的值。根据下拉列表中选中的选项,它会填充另一个下拉列表中的值。
问题:
例如:在第一个jQuery multiselect下拉列表中,如果我选择3个值并根据检查的这三个值关闭下拉列表,则会在另一个jQuery multiselect下拉列表中获取详细信息,但仅检查最后一个选中的值并保留。我认为这是因为按钮单击事件它发生异步回发并且值不保留。他们可以选择在按钮点击事件中阻止回发,但应该调用点击,然后只保留值。
对于这些我附加屏幕截图:
根据这三个值,它会被加载到另一个下拉列表中,但它会显示" 1选择"这是默认的"选择"。
但是第二个下拉列表我能够获得在第一个下拉列表中检查的值:
这是我的javascript:
$(document).ready(function () {
$('.department').multiselect({
show: ["bounce", 5], hide: ["blind", 1],
close: function () {
debugger;
var values = new Array();
$(this).multiselect("getChecked").each(function (index, item) {
values.push($(item).val());
});
$("input[id*=selectedValues]").val(values.join(","));
document.getElementById("<%=hdnDepartment.ClientID %>").value = values;
if (document.getElementById("<%=hdnDepartment.ClientID %>").value != "") {
**$("#<%=Button1.ClientID %>")[0].click();**
}
}
})
.multiselectfilter();
});
protected void Button1_Click(object sender, EventArgs e)
{
Populate();
}
如果我错了,请纠正我......
答案 0 :(得分:1)
您可以使用OnClientClick并根据您的要求返回false或true。 当OnClientClick返回true时会发生回发,否则没有回发
答案 1 :(得分:0)
<script type="text/javascript">
function pageLoad() {
$("#<%= DepartmentsListBox.ClientID %>").multiselect({
create: function () {
$(this).data("initValues", $.map($("option:selected", this), function (option) { return option.value; }).join(","));
},
close: function () {
var selectedValues = $.map($("option:selected", this), function (option) { return option.value; }).join(",");
if (selectedValues !== $(this).data("initValues")) {
__doPostBack("<%= DepartmentsListBox.UniqueID %>", "");
}
}
});
$("#<%= EmployeesListBox.ClientID %>").multiselect();
}
</script>
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:ListBox runat="server" ID="DepartmentsListBox" DataValueField="DepartmentId"
SelectionMode="Multiple" DataTextField="Name" Width="300px"></asp:ListBox>
<br />
<asp:ListBox runat="server" ID="EmployeesListBox" Width="300px" SelectionMode="Multiple" AppendDataBoundItems="true"
DataValueField="EmployeeId" DataTextField="Name"></asp:ListBox>
<br />
<asp:Button runat="server" ID="SubmitButton" Text="Submit" />
</ContentTemplate>
</asp:UpdatePanel>
服务器代码
private static Random rnd = new Random();
private string[] selectedDepartments;
private string[] SelectedDepartments
{
get
{
return selectedDepartments ?? (selectedDepartments = DepartmentsListBox.Items.OfType<ListItem>().Where(item => item.Selected).Select(item => item.Value).ToArray());
}
}
protected void Page_Init(object sender, EventArgs e)
{
EmployeesListBox.PreRender += new EventHandler(EmployeesListBox_PreRender);
DepartmentsListBox.SelectedIndexChanged += new EventHandler(DepartmentsListBox_SelectedIndexChanged);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDepartments();
}
}
void EmployeesListBox_PreRender(object sender, EventArgs e)
{
if (EmployeesListBox.Items.Count > 0)
{
EmployeesListBox.Attributes.Remove("disabled");
}
else
{
EmployeesListBox.Attributes["disabled"] = "disabled";
}
}
private void BindDepartments()
{
var departments = Enumerable.Range(1, 10)
.Select(id => new { DepartmentId = id, Name = "Department " + id.ToString() });
DepartmentsListBox.DataSource = departments;
DepartmentsListBox.DataBind();
}
void DepartmentsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var employees = Enumerable.Range(1, 10)
.Select(id => new { EmployeeId = id, DepartmentId = rnd.Next(1, 10), Name = "Employee #" + id.ToString() })
.Where(emp => SelectedDepartments.Contains(emp.DepartmentId.ToString())).ToList();
for (int itemIndex = EmployeesListBox.Items.Count - 1; itemIndex >= 0; itemIndex--)
{
var employee = employees.FirstOrDefault(emp => emp.EmployeeId.ToString() == EmployeesListBox.Items[itemIndex].Value);
if (employee == null)
{
EmployeesListBox.Items.RemoveAt(itemIndex);
}
else
{
employees.Remove(employee);
}
}
EmployeesListBox.DataSource = employees;
EmployeesListBox.DataBind();
}