views:

333

answers:

2

Hiya i'm creating a web form and i want a user to be able to make certain selections and then add the selections to a text box or listbox.

Basically i want them to be able to type someone name in a text box ... check some check boxes and for it up date either a text for or a list box with the result on button click...

e.g. John Smith Check1 Check3 Check5

any help would be great .. thanks

A: 

I will show you a basic example of a TextBox, Button and a ListBox. When the button is clicked the text will be added to the 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);
}

If you want to use javascript / jquery to do this your could just omit the server side event and just add the following function to the Click property of the button.

$(document).ready(function()
{
    $("#yourButton").click(function()
    {
        $("#yourListBox").append(
            new Option($('input[name=yourTextBox]').val(),
                'Add value here if you need a value'));
    });
});
Kelsey
A: 

Lets Suppose you have a gridview which is being populated on Searching happened using Textbox.

Gridivew got some checkboxes and after selection of these checkboxes you wanted to add into listbox

Here is the javascript which will help you to add into listbox.

Please modify as per your requirement,I have less time giving you only a 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);

}

}

}

DBMaster