簡體   English   中英

帶有VB的ASP.NET。 ListBox添加和刪除項目

[英]ASP.NET with VB. ListBox adding and removing items

嗨,我正在創建一個Web表單,我希望用戶能夠進行某些選擇,然后將選擇添加到文本框或列表框。

基本上我希望他們能夠在文本框中鍵入某人的名字...選中一些復選框,並針對其進行更新,或者是文本的顯示,還是列表框的單擊結果...

例如John Smith Check1 Check3 Check5

任何幫助都會很棒..謝謝

我將向您展示TextBoxButtonListBox的基本示例。 單擊該按鈕后,文本將添加到列表框中。

// 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'));
    });
});

假設您有一個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);

}

}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM