Solved

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

Posted on 2008-06-25
8
3,817 Views
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.appendChild(txt);

        newOpt.setAttribute('value', tval);

        //newOpt.text = tval;

        //newOpt.value = tval;

        list.appendChild(newOpt);

	}

</script>
 

<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

0
Comment
Question by:sbornstein2
  • 4
  • 3
8 Comments
 
LVL 21

Accepted Solution

by:
naspinski earned 250 total points
Comment Utility
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.
0
 

Author Comment

by:sbornstein2
Comment Utility
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.
0
 
LVL 21

Expert Comment

by:naspinski
Comment Utility
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.
0
 

Author Comment

by:sbornstein2
Comment Utility
ok so ya this would work and then I can use:

StringBuilder sb = new StringBuilder();
            foreach (ListItem item in lstBulletPoints.Items)
            {
                try
                {
                    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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 21

Expert Comment

by:naspinski
Comment Utility
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.
0
 
LVL 39

Assisted Solution

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

To add values thorugh asp.net

  myList.Items.Add("3");
                    myList.Items.Add("4");
0
 
LVL 21

Expert Comment

by:naspinski
Comment Utility
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"
0
 

Author Closing Comment

by:sbornstein2
Comment Utility
both were helpful thanks guys.  I might use AJAX and a server control instead but nevertheless thanks for the help and awesome info
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now