ASP.Net/C# - How to get value of a regular HTML Select List Box in code behind?

Posted on 2008-06-25
Last Modified: 2012-08-14
Hello all I could really use some help here.  I have a ASPX page that has a regular select list on it where the user can add and remove items via javascript.  I need to in my code behind on a save loop through each value that was entered into that select list to create a semi-colon seperated string.  Then the same coming back into the page I need to parse through the values and add to the list box.  I know how to handle the for loop for the coming back with a string split.  I just need to know how to add the item to the select list HTML.  Any help would be much appreciated.  Attached is the code from the page.
<script type="text/javascript" language="javascript">
	function remove()
		var list = window.document.getElementById('myList');
		var keepLooking = true;
		while ( keepLooking ) {
			list.remove( list.options.selectedIndex );
			if ( list.options.selectedIndex < 0 ) {
				keepLooking = false;
	function add()
		var list = window.document.getElementById('myList');
		var tbox = document.getElementById('myText');
		var tval = tbox.value;
        var newOpt = document.createElement('option');
        var txt = document.createTextNode(tval);
        newOpt.setAttribute('value', tval);
        //newOpt.text = tval;
        //newOpt.value = tval;
<select id="myList" name="myList" size="10" style="width:200px;"></select>
<a href="javascript:remove();">Remove</a><br />
<input type="text" id="myText" value="" style="width:200px;"/>
<a href="javascript:add();">Add</a>

Open in new window

Question by:sbornstein2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 21

Accepted Solution

naspinski earned 250 total points
ID: 21864804
Instead, why don't you make this an asp:dropdownlist and change the javascript to accommodate the newly generated IDs (view source).  Ass the asp controls just make an html drop list anyways, jsut takes care of a lot of the footwork of getting it to the backend.

Author Comment

ID: 21864937
ya that is what I was thinking but im not sure how to do that.  Do I use just the name of the listbox? and also the other problem is I just need to make sure it does not post back.
LVL 21

Expert Comment

ID: 21864975
If it does not postback, there is no way to get the codebehind (c#) the value, as that is server side.

So you need a postback... you could use an ajax asp:updatepanel for an asynch postback.

Also, to get the asp-made ids, just run your page, go into your browser and view source.  Then find the dropdowns and you can see the ID that asp made for them... use those.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 21865002
ok so ya this would work and then I can use:

StringBuilder sb = new StringBuilder();
            foreach (ListItem item in lstBulletPoints.Items)
                    sb.Append(item.Text.ToString() + ";");
                catch (Exception)
                    // ignore
that will create my list.

Can that ID ever change though and is there a better way to use like a client id or something
LVL 21

Expert Comment

ID: 21865033
The ID will never change.  It is a bit of a pain, but in order to allow both server and client-side scripts to work on the same objects, you have to coax them into working sometimes.
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 250 total points
ID: 21865043
<select id="myList" name="myList" size="10" style="width:200px;" runat="server"></select>

To add values thorugh

LVL 21

Expert Comment

ID: 21865074
wow... i feel like an idiot since I didn't even think of making it a server control :P  good call pratima

Just have to make sure it is inside of a form tag that is also runat="server"

Author Closing Comment

ID: 31470523
both were helpful thanks guys.  I might use AJAX and a server control instead but nevertheless thanks for the help and awesome info

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question