从代码隐藏中的选定列表框项中检索属性数据
Retrieving attribute data from selected ListBox items in code behind
我有一个使用 asp.net 和 C# 的网络应用程序。我有一个列表框,用户可以在其中 select 多个项目。它们使用属性 属性 进行分组。我需要在按钮单击事件后面的代码中使用此 属性。我以为我可以在客户端设置属性值,它们将在服务器端可用,但我了解到情况并非如此。
我不知道解决这个问题的最佳方法。每个 ListItem 都有我希望在服务器端拥有的名称、值和组。名称和值在服务器端已经可用。我需要与每个 selected 项目关联的组。我应该为每个 selected 项目创建一个隐藏字段吗?是否应该有一个隐藏字段,其中包含与每个分组相关联的分组和值?我有一个设置分组属性的 jquery 函数。我想用它来设置隐藏字段,但我不确定我是应该使用一个隐藏字段还是使用多个项目 selected。
这是我已经拥有的 javascript:
$(document).ready(function () {
//Create groups for recipient dropdown list
$(".chosen-select option[grouping='GlobalGroups']").wrapAll("<optgroup label='Global Groups'>");
$(".chosen-select option[grouping='PersonalGroups']").wrapAll("<optgroup label='Personal Groups'>");
$(".chosen-select option[grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");
//Configure the ListBox using the 'chosen' jquery plugin
$(".chosen-select").chosen({
search_contains: true,
no_results_text: "Sorry, no match!",
allow_single_deselect: true
});
$('.chosen-container').css('width', '600px');
//set attribute property for selected list
$(".chosen-select").chosen().change(function (evt) {
$(".chosen-select").find("option:selected").each(function () {
var label = $(this).closest('optgroup').prop('label');
if (label == "Global Groups") {
$(this).attr("grouping", "GlobalGroups");
}
else if (label == "Personal Groups") {
$(this).attr("grouping", "PersonalGroups");
}
else {
$(this).attr("grouping", "Individuals");
}
});
});
});
这是HTML:
<asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple"
data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
</asp:ListBox>
对于任何有此问题的人...我使用了一个隐藏字段 asp:HiddenField,并将所有选择添加到一个以分号分隔的字符串中。
我分析了后面代码中的字符串,以确定收件人是团体还是个人。
这是我最后的 jquery 脚本:
$(".chosen-select").chosen().change(function (evt) {
$("#hdnRecipientAttr").val("");
$(".chosen-select").find("option:selected").each(function () {
var label = $(this).closest('optgroup').prop('label');
var currentHdnValue = $("#hdnRecipientAttr").val();
if (label == "Individuals") {
var attrText = "Individuals-" + $(this).prop('value') + ";";
$("#hdnRecipientAttr").val(currentHdnValue + attrText);
}
else {
var attrText = "Group-" + $(this).prop('value') + ";";
$("#hdnRecipientAttr").val(currentHdnValue + attrText);
}
});
//remove ending semicolon
var hdnValue = $("#hdnRecipientAttr").val();
$("#hdnRecipientAttr").val(hdnValue.slice(0, -1));
});
我有一个使用 asp.net 和 C# 的网络应用程序。我有一个列表框,用户可以在其中 select 多个项目。它们使用属性 属性 进行分组。我需要在按钮单击事件后面的代码中使用此 属性。我以为我可以在客户端设置属性值,它们将在服务器端可用,但我了解到情况并非如此。
我不知道解决这个问题的最佳方法。每个 ListItem 都有我希望在服务器端拥有的名称、值和组。名称和值在服务器端已经可用。我需要与每个 selected 项目关联的组。我应该为每个 selected 项目创建一个隐藏字段吗?是否应该有一个隐藏字段,其中包含与每个分组相关联的分组和值?我有一个设置分组属性的 jquery 函数。我想用它来设置隐藏字段,但我不确定我是应该使用一个隐藏字段还是使用多个项目 selected。
这是我已经拥有的 javascript:
$(document).ready(function () {
//Create groups for recipient dropdown list
$(".chosen-select option[grouping='GlobalGroups']").wrapAll("<optgroup label='Global Groups'>");
$(".chosen-select option[grouping='PersonalGroups']").wrapAll("<optgroup label='Personal Groups'>");
$(".chosen-select option[grouping='Individuals']").wrapAll("<optgroup label='Individuals'>");
//Configure the ListBox using the 'chosen' jquery plugin
$(".chosen-select").chosen({
search_contains: true,
no_results_text: "Sorry, no match!",
allow_single_deselect: true
});
$('.chosen-container').css('width', '600px');
//set attribute property for selected list
$(".chosen-select").chosen().change(function (evt) {
$(".chosen-select").find("option:selected").each(function () {
var label = $(this).closest('optgroup').prop('label');
if (label == "Global Groups") {
$(this).attr("grouping", "GlobalGroups");
}
else if (label == "Personal Groups") {
$(this).attr("grouping", "PersonalGroups");
}
else {
$(this).attr("grouping", "Individuals");
}
});
});
});
这是HTML:
<asp:ListBox ID="lstBoxTo" runat="server" SelectionMode="Multiple"
data-placeholder="Choose recipient(s)…" multiple="true" class="chosen-select">
</asp:ListBox>
对于任何有此问题的人...我使用了一个隐藏字段 asp:HiddenField,并将所有选择添加到一个以分号分隔的字符串中。 我分析了后面代码中的字符串,以确定收件人是团体还是个人。 这是我最后的 jquery 脚本:
$(".chosen-select").chosen().change(function (evt) {
$("#hdnRecipientAttr").val("");
$(".chosen-select").find("option:selected").each(function () {
var label = $(this).closest('optgroup').prop('label');
var currentHdnValue = $("#hdnRecipientAttr").val();
if (label == "Individuals") {
var attrText = "Individuals-" + $(this).prop('value') + ";";
$("#hdnRecipientAttr").val(currentHdnValue + attrText);
}
else {
var attrText = "Group-" + $(this).prop('value') + ";";
$("#hdnRecipientAttr").val(currentHdnValue + attrText);
}
});
//remove ending semicolon
var hdnValue = $("#hdnRecipientAttr").val();
$("#hdnRecipientAttr").val(hdnValue.slice(0, -1));
});