【发布时间】:2016-06-18 18:48:02
【问题描述】:
我正在做一个项目,目前我正在做一个属于发布文章页面的模块。在此页面中,我们可以选择使用文本框和按钮添加关键字。
我通过使用 jQuery 成功将文本框输入关键字添加到列表框来完成此操作。
如果用户在将任何特定关键字添加到列表框后想要删除它,用户可以通过在列表框中选择该关键字来删除它,然后会出现一个删除按钮。如果用户单击该 remove button 关键字从列表框中删除。
问题:
使用 jQuery 成功将关键字添加到列表框后,我无法在 C# 代码后面访问该列表框项目
Asp.net 页面列表框:
<a id="arkyw" href="#/"><font color="#cc0000">Remove Keyword</font></a>
<span id="spKeyword" style="color:#e74c3c;"></span>
<asp:ListBox ID="listBoxKeywords" runat="server" CssClass="list-group" style="border:0px; overflow-y:hidden; height:83px; width:300px;">
jQuery:
var count = [];
var i = 0;
$("#btnAddKeyword").click(function () {
var txt = $("#txtkeyword").val();
$('[id$=listBoxKeywords]').show();
if (jQuery.inArray(txt, count) != "-1") {
$("#spKeyword").text('Keyword alread exists');
} else {
var listCount = $('[id$=listBoxKeywords] option').length;
if (listCount <= 4) {
count[listCount] = txt;
var alreadyExist = false;
$('[id$=listBoxKeywords] option').each(function () {
if ($(this).val() == txt) {
$("#spKeyword").text('Keyword alread exists');
alreadyExist = true;
return;
}
});
if (!alreadyExist) {
$('[id$=listBoxKeywords]').append($('<option></option>').attr('value', count[listCount]).text(txt));
}
} else {
$("#spKeyword").text('5 Keyword limit exceed');
}
}
});
//Remove Value from the List
$('[id$=listBoxKeywords]').click(function (e) {
var $target = $(e.target);
if ($target.is('option')) {
$("#arkyw").show();
}
});
$("#arkyw").click(function () {
var a = $('[id$=listBoxKeywords] option:selected').val();
var id = $('[id$=listBoxKeywords] option').length;
//alert('ListCount : ' + a);
//alert('Total List Item : ' + id);
//alert('After Remove : ' + count[a-1]);
count = jQuery.grep(count, function (e) {
$('[id$=listBoxKeywords] option:selected').remove();
return e != a;
});
});
如何在后面的 C# 代码中访问列表框项?
【问题讨论】:
-
<asp:ListBox是 Web 表单,而不是 MVC
标签: c# jquery asp.net webforms listbox