【问题标题】:ASP.NET with VB. ListBox adding and removing items带有 VB 的 ASP.NET。 ListBox 添加和删除项目
【发布时间】:2009-11-19 16:33:07
【问题描述】:
您好,我正在创建一个网络表单,我希望用户能够做出某些选择,然后将选择添加到文本框或列表框。
基本上,我希望他们能够在文本框中输入某人的姓名...选中一些复选框并为其更新文本或列表框,并在按钮单击时显示结果...
例如
约翰·史密斯 Check1 Check3 Check5
任何帮助都会很棒..谢谢
【问题讨论】:
标签:
asp.net
javascript
vb.net
textbox
listbox
【解决方案1】:
我将向您展示TextBox、Button 和ListBox 的基本示例。单击按钮时,文本将添加到列表框中。
// in your .aspx file
<asp:TextBox ID="yourTextBox" runat="server" /><br />
<asp:Button ID="yourButton" runat="server" Text="Add" OnClick="yourButton_Click" /><br />
<asp:ListBox ID="yourListBox" runat="server" /><br />
// in your codebehind .cs file
protected void yourButton_Click(object sender, EventArgs e)
{
yourListBox.Items.Add(yourTextBox.Text);
}
如果您想使用 javascript / jquery 来执行此操作,您可以省略服务器端事件,只需将以下函数添加到按钮的 Click 属性。
$(document).ready(function()
{
$("#yourButton").click(function()
{
$("#yourListBox").append(
new Option($('input[name=yourTextBox]').val(),
'Add value here if you need a value'));
});
});
【解决方案2】:
假设您有一个 gridview,它在使用 Textbox 进行搜索时填充。
Gridivew 有一些复选框,选择这些复选框后,您想添加到列表框中
这是帮助您添加到列表框的 javascript。
请根据您的要求进行修改,我没有多少时间只给您一个javascript。
function addItmList(idv,valItem) {
var list =document.getElementById('ctl00_ContentPlaceHolder1_MyList');
//var generatedName = "newItem" + ( list.options.length + 1 );
list.Add(idv,valItem);
}
function checkitemvalues()
{
var gvET = document.getElementById("ctl00_ContentPlaceHolder1_grd");
var target = document.getElementById('ctl00_ContentPlaceHolder1_lstIControl');
var newOption = window.document.createElement('OPTION');
var rCount = gvET.rows.length;
var rowIdx = 0;
var tcount = 1;
for (rowIdx; rowIdx<=rCount-1; rowIdx++) {
var rowElement = gvET.rows[rowIdx];
var chkBox = rowElement.cells[0].firstChild;
var cod = rowElement.cells[1].innerText;
var desc = rowElement.cells[2].innerText;
if (chkBox.checked == true){
addItmList(rowElement.cells[1].innerText,rowElement.cells[2].innerText);
}
}
}