How do you pre-select values in a multiple selection list box

Let's say I have a list box like so, this is already rendered:

<select name="names" size="3" multiple="multiple" id="names1" >
<option value="0">John</option>
<option value="1">Mike</option>
<option selected="2">Bill</option>

how could I pre-selecte two names on load, if I say
names1.selectedvalue = "1" it pre-selects Mike cool
but if I say names1.selectedvalue = "1,2" then I get an exception.
Is there a way to pre-select more than 1 value?
thanks for your help
3/2/2005 10:52:18 PM 93655 articles. 6 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 4

hi, i solved it by looping trough every ListItem in ListBox.Items and checked  the value of each ListItem against the array? of id's to select...
3/2/2005 11:23:30 PM
Here is the sample code. This is select first, third and fifth items.

aspx code

<asp:ListBox SelectionMode="Multiple" Rows="10" ID="test" Runat="server"></asp:ListBox>


protected System.Web.UI.WebControls.ListBox test;

private void Page_Load(object sender, System.EventArgs e)
int i=0;
foreach (ListItem li in test.Items)
if (i==0 || i==2 || i==4)

Rules at work:
1) Boss is always right
2) If Boss is wrong; refer rule #1
3/2/2005 11:42:00 PM

ListBox1.Items[0].Selected = true;
ListBox1.Items[1].Selected = true;
or a loop...
This posting is provided "AS IS" with no warranties, and confers no rights.
We Are Hiring
3/2/2005 11:42:29 PM
thank you for your help guys, good solutions;)
3/3/2005 12:27:57 AM

I assume you're using asp:ListBox, asp:DropDownList, or at least a runat="server" in your control tag; otherwise the above (or below, depending on you sort your posts) code will not work.
If you need to select different values, be sure to call the ClearSelection() method first.
FYI, the original method you tried to use is available in EasyListBox. It's just one of the many features that makes it a *lot* more intuitive and easy to use than the native listbox/dropdown controls.
Peter Brunone
Do the impossible, and go home early.
3/3/2005 11:07:36 PM

Similar Artilces:

Load Text box of one web form with selected list item of list box of another web form
one help please Problem is i have two web forms  1.  ForiegnkeyTest.aspx  2. foroginkeytable.aspx   step 1 1 ForiegnkeyTest.aspx one web form <head runat="server"><script language="javascript" type="text/javascript">   function popWin()   {         var confirmWin = null;      confirmWin ='foroginkeytable.aspx','anycontent',   'width=455,height=435,status');   }  </script> &...

selecting multiple values in a list box using code
Hi, I have a list box with certain values. The SelectionMode property of List box is set to Multiple. I want to select multiple items based on the values of the items in the Listbox using the code. Can any one please help.Thanks eg: for i = 0 to listbox.items.count - 1 if listbox.items(i).selected then Response.Write("Selected") else Response.Write("Selection Needed") end if next refer links:

passing multiple select (selection list on a form) to html datawindow.
How do I pass a multi-select list box items on a form to a html datawindow as a page parameters? appreciate if someone can help. Thanks Syed. I found several ways to do this on if you want to search there for details. Try using client scripting and pass multiple parameters, one for each selected element in the list box. -- Larry Cermak [Team Sybase] Corporate Technology Partners, Inc. Web DataWindow Articles ( TechWave2000 Presentations AM34, Extending the Web DataWindow (Mon-T...

showing pre selected values in list box....
 have listbox with DataSourceId= [ObjectDataSource]... like this in my aspx page.. My ASPX page... <asp:ListBox ID="lbxUsers" CssClass="selectBox" runat="server" DataSourceID="odsGetAllUsers" DataTextField="user_name" DataValueField="user_id" SelectionMode="Multiple"></asp:ListBox> <asp:ObjectDataSource ID="odsGetAllUsers" runat="server" SelectMethod="GetAllIUsers" </asp:ObjectDataSource> My ASPX.CS page.. protectedvoid Page_Load(object sender, EventArgs e) { lbxUsers.Items.FindByValue("3").Selected = true; lbxUsers.Items.FindByValue("1").Selected = true...

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

problem with selected text field and selected value field in check box list
Hi! i have a  problem with check box list:here is the code for binding the check box list with sqldatasource  SqlDataSource1.SelectCommand = "select vcentrolno,inttpaperno from tabmarksdetails where vcbunchno='" & strBunchNo & "'"  chkEnrolNos.DataBind()say now my chkEnrolNos consist of data in this way:vcentrolno        inttpaperno selected text   selected valueA11439            1A11439            2AR439    ...

Save Multiple Selected Values When Using List Box
Hi, I'm using a listbox within a formView control, this list box allows for multiple selections - how can I save all the selected values not just the first one - Many thanks Marc You have to loop thouth the ListBox items..  string ListItemSelected = ""; foreach (ListItem li in ListBox1.Items) { if (li.Selected == true) { ListItemSelected = ListItemSelected + li.Value + ","; } }  My Blog"Don't be afraid to be wrong; otherwise you'll never be right."...

Problem in selecting text in List box Web Form
hi In List box load the data from database. If i am select any Item from list box i am geting selected Indexvalue ="-1" at the same time i am not get the text value. how can i get the selected index text value. Regards, Sabarish. Hi, it sounds to me that you're rebinding your data upon every postback. If you're doing it in code behind place it between if(!Page.IsPostBack) to avoid this. Then you should be able to get a value different than -1 (which means nothing's selected). Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for...

Second SELECT-box doesn't show the selected value as SELECTED
Hi! I usually find what I look for in the other threads, but now I've spent two weeks searching and I still can't work this out, even though I'm sure the solution is easy:( Why doesn't my second dropdown-list show the selected value Arb (which shows in the url-window as for example myPage.asp?Ar=1976&Arb=1987) as selected? It manages to use the Ar-variable for the first SELECT-box, but the second box always has the first querystring-value selected, as if Arb="". I've also tried using a second sql-query, doing exactly as with the first SELECT-box, without success. Is ...

Selected value in select list with javascript based off value in database
 I've got a classic asp page with a select list. I need the select list's selected value to be whatever the user selected when they created their account. I've been trying to use <body onLoad="onLoad();"> to call my function, and then I'm trying to specify which value should be selected in the function. Also, when the select list selected values change, there is a javascript function I call to disable text boxes. I think there is an issue with my javascript function, and I can't find any examples of this from good ole' google.  My j...

Populate 2nd drop-down list box based on selected values in 1st drop-down list box
Hi Group,I have two DDL boxes on my page.  DDL one has a list of values and a code.e.g code  ValueABC   123ABC   345FGH   123 DDL 2 should only show values for the code selected in  DDL1 e.g  if user selects record 1 with code ABCthen DDL2 should show ALL records with the linked field value of ABC.The tables are normalized.  For your info, the name of the field of the value I would like to check is called sublistcode.Using  TIA         Try this code: Note: Set appenddataboun...

How to store multiple values selected from a list box as separate records in a database???
Hi , can somebody plz help me out in this... My project consists of 2 parts...first where the user enters wht project he wants us to do & in the later part the director assign the staff for doing the project ... once the user submits the project i grab the project id from my database which consists of one table and try to update that project with the assigned staff...but the problem is that the project can be assigned to many people for which i need i have a multiple select list box... i am able to grab the multiple staff name but unable to understand how i can store it as s...

Form: Focus on selected values in dropdownlists, Refresh after selection and hidding formfields based on selection.
Hello again,I have a form with several dropdown lists. Depending on the selected value in the dropdown lists other fields become available in that same form. It is working in classic ASP in combination of JavaScript. I want to migrate it to ASP.NET. I have posted a part of the form at the end of this post.My questions:1. I use <option <%If iFood="0" then%>selected<%End If%> value="0">No value selected</OPTION> to select the selected item when the form is reloaded after the selecting. I know that this also works in ASP.NET, but isn't there a typical ASP....

Cascading Dropdown List
Hi, First post so hello and need some help with an issue I have been dealing with.  I have an intranet page I created in ASP.NET using C# .NET 3.5 with a few Ajax Extenders including 3 Cascading Dropdown Lists. Now I have used the Cascading dropdown lists for my own purposes i.e they do not actually cascade their results to another dropdown list, the reason I did this was due to the having a User Control that contains an update panel and other bits i.e I didn't want the control/page posting page (something that happens when you click on a regular dropdown list). Everything is wor...

Web resources about - How do you pre-select values in a multiple selection list box -

Charlie Clark Select Pre-Owned - Brownsville, TX
Charlie Clark Select Pre-Owned has been at this location 1 1/2 yrs, helping people get in their new Used Vehicle. Credit is NOT a Problem. We ...

Bartolotta's Select Pre-Owned Opens New Facility
Bartolotta's Select Pre-Owned Opens New Facility Mysullivannews For owner Tom Bartolotta and Manager Danny Hawkins, the new opening of Bartolotta's ...

Press Select During Ninja Gaiden 3 To Bring Up A Movelist
Team Ninja have been tweeting about Ninja Gaiden 3.

Ex-Supreme Court judge on the panel to select Press Watchdog
More than 90 per cent of national newspapers and the vast majority of the regional press, along with major magazine publishers, have signed up ...

Select Retailers Offering OUYA Pre-Orders
... little Android-based console that raised millions in crowd-funding will be available in select retail stores, with some even offering pre-orders. ...

Pre-orders, pre-downloads for 'select' titles coming to Xbox One
... copies of high-profile games as soon as they launch is about to become a little easier on Xbox One: during Microsoft's Gamescom 2014 presentation, ...

Apple TV 4 and iPad Pro Pre-Orders Begin at Select Authorized Resellers in Europe
Just weeks before the Apple TV 4 is expected to be released worldwide, select Apple Authorized Resellers in Europe have begun accepting pre-orders ...

Consumer Discretionary Select Sector SPDR ETF (XLY) Stock Press Releases and Filings - Seeking Alpha
... Alpha Home - Portfolio - Market Currents - Investing Ideas - Dividends & Income - ETFs - Macro View - Alerts XLY Consumer Discretionary Select ...

Press regulation: commissioner selects appointments panel members
Government presses on with royal charter regulator

ASUS C200 Chromebook Available For Pre-Order From Select Stores
... device to arrive on the streets this coming May for $250 a pop, where interestingly enough, it is already up for pre-order over at select stores, ...

Resources last updated: 1/12/2016 5:24:50 PM