Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 22nd, 2009, 11:32 PM
Authorized User
Join Date: Aug 2008
Location: manila, , Philippines.
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
Default updata listbox in code behind

I have a server control listbox and Button. When i click on button6 i want to add item in listbox3 thru client script. Problem is i also want to
add item to that listbox in code behind from an ongoing thread.... I've researched and learned that i need to use a hidden field so i can update
the listbox in my client script..i tried it but so difficult is there an easy way to work this problem out... Thanks

Code Behind

protected void Page_Load(object sender, EventArgs e)

Button6.Attributes.Add("onclick", "AddItem();return false;");

private void OnMessage(object sender, jabber.protocol.client.Message msg)

function AddItem()
var opt=document.createElement("option");
var msg=document.getElementById('TextBox3');
var labname=document.getElementById('Label1');

document.getElementById("ListBox3").options.add(op t);
opt.text=labname.innerText+": "+msg.value;


<asp:Button ID="Button6" runat="server" BackColor="Gray" ForeColor="#CCCCCC" Text="Send" Width="60px" />
<asp:ListBox ID="ListBox3" runat="server" BackColor="#EAEAEA" Height="180px"
style="margin-top: 0px" Width="251px" AutoPostBack="True">
  #2 (permalink)  
Old February 23rd, 2009, 08:29 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

You were on the right track with the bit of JavaScript you had. To add items to a listbox through a clientside script you would do something like:

javascript Code:
function addNewOption()
  var labelText = document.getElementById('Label1').innerText'
  var msg= document.getElementById('
  var optionText = labelText + '
' + msg; 
  var optionValue = msg;

  var objOption = new Option(optionText, optionValue)
  var listObject = document.getElementById('
  var itemPosition = listObject.length;
  listObject.options[itemPosition] = objOption;
  return false;

Now there are a few problems with this code. First off you are referencing your Server controls with the value that you are supplying for the ID property. The problem here is that your controls may not be named this on the clientside when they are ultimately emitted and sent to the browser. For example, a textbox in a user control may actually wind up with an ID of: UserControl_TextBox1! To circumvent this problem you should get used to using syntax such as:

var listObject = document.getElementById('<%=ListBox1.ClientID%>');

This will ensure that the proper ID is passed in to your javascript.

Next, since you made the comment about a hidden field, you probably already know that items that are added to the select list in this manner are not accessible in your code behind since they do not exist in viewstate (although you SHOULD be able to access the values that were selected via the POST through a call to Request.Form["Key"]). In any event, if you want to append the values to a hidden field you would use a javascript similar to the following:

javascript Code:
function selectOnChange()
    var textBox = document.getElementById('textHide');
    var listObject = document.getElementById('test');
    textBox.value = '';
 for(i=0; i <= listObject.options.length - 1; i++)
   textBox.value = textBox.value + listObject.options[i].value + ', ' ;

-I tested all code in IE 7.0 without a problem.
-Becareful with the <% %> script blocks inside of your javascripts. This can cause errors to be thrown in the manner of 'Can not change the control collection because the code contains <% %>'. By and large I see this error when there is an AJAX framework at play and various user controls on a page.

Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple Listbox values in another listbox terryv Excel VBA 0 June 27th, 2007 07:01 AM
Listbox alastair Access 7 January 25th, 2006 05:53 PM
updata bukky Classic ASP Databases 2 February 27th, 2004 02:42 PM
I'm back :) Listbox var from listbox MichaelTJ .NET Web Services 2 October 21st, 2003 07:06 PM

All times are GMT -4. The time now is 02:09 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.