Dynamic DropDownList Default Selected Item Does Not Select Default Item

I have some dynamic listbox code that creates a listbox based on information stored in a database. The database access works fine. The only issue is that when I use the lbNew.Items.FindByValue(...).Selected = True line, no default select is made. The listbox just defaults to the first item in the list.

lbNew = New DropDownList

' Scans for the list source.
' If "values_table" returns the first character = '@',
' then a table is the datasource.
strDataSourceTable = Nothing
strDataSourceTable = objDataReader.Item("values_table")

' If there is a data list, display the item, else ignore
If Not strDataSourceTable = Nothing Then
    If strDataSourceTable.Chars(0) = "@" Then
	' Extract the table and column title
	strDataSourceTable = strDataSourceTable.Remove(0, 1)
	strDataSourceTableSplit = PassdownFunctions.SplitString(strDataSourceTable, "|")
	strDataSourceTableName = strDataSourceTableSplit.Item(0).ToString
	strDataSourceTableCol = strDataSourceTableSplit.Item(1).ToString
	' Open the database
	objConnection2 = New OleDbConnection(connString)
	strSQL2 = "SELECT * FROM [" & strDataSourceTableName & "] ORDER BY " & strDataSourceTableCol & " ASC;"
	objCommand2 = New OleDbCommand(strSQL2, objConnection2)
	objDataReader2 = objCommand2.ExecuteReader()
	' Extract the list items
	strListboxItems = New ArrayList
	While objDataReader2.Read()
	End While
	' Close database
	strListboxItems = New ArrayList
	strListboxItems = PassdownFunctions.SplitString(strDataSourceTable, ";")
    End If
    lbNew.ID = "listbox^" + objDataReader.Item("item_name") + "^" + n
    lbNew.CssClass = "passdown_input_item"
    'lbNew.SelectionMode = ListSelectionMode.Single
    lbNew.DataSource = strListboxItems
	lbNew.Items.FindByValue(objDataReader.Item("default")).Selected = True
    Catch ex As Exception

    End Try
    'lbNew.Rows = CType(objDataReader.Item("height"), Integer)
    lbNew.Width = CType(objDataReader.Item("width"), Integer)
    lbNew.Attributes.Add("style", "left: " & CInt(objDataReader.Item("x")) & "px; top: " & CInt(objDataReader.Item("y")) & "px;")
    lbNew.Attributes.Add("onchange", "cstylec(this,'" & objDataReader.Item("default") & "');")
End If

Note that all values pulled from the database have been verified as correct, so that does not seem to be the issue.

1/11/2006 6:51:53 AM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 6

Two things come to mind:

1)  You have a try/catch but no way to report that the exception was thrown; what happens when you Response.Write the exception.message (or something a bit more best-practice if you don't like Response.Write)?

2)  Have you tried writing out objDataReader.Item("default") to make sure the value is what you expect it to be?


Peter Brunone
Founder, EasyListBox.com
Do the impossible, and go home early.
1/11/2006 4:47:46 PM

Sorry about getting back so late. It's the weekend for me.

1) I used the try/catch statement to keep the program from erroring when it was accessing something invallid. I origionally had it outputting to the MsgBox() fuction on error, but was not getting an error. I then moved the line to the Try portion, and that was going through, so I got rid of that line. I should have a Msg.Text addition in the Catch portion, to be sure.

2) I did also change the items to actual string values that are located in the ArrayList that is being bound, but this did nothing either.

A very currious problem indeed.

1/14/2006 8:56:40 AM
I didn't mean the ArrayList values (since it seemed you'd already
checked those); I meant the value you're trying to assign as the
selected value.  Is that one correct?

Peter Brunone
Founder, EasyListBox.com
Do the impossible, and go home early.
1/14/2006 7:48:27 PM
Aye. It is the correct one. I used combinations of MsgBox() outputs and Msg.Text outputs to verify that these were actually pulling the correct strings from the database. They all matched. I also staticly defined the FindByValue(...) string, and that did not error, returning the correct string.
1/15/2006 3:41:33 AM

Similar Artilces:

After selecting DropDownList Item, how to set DropDownList Item so that it corresponds to new selection.
Hi,  I'm having difficulty retaining DropDownList selected item so that it corresponds to the new page that is called when the item is selected. i.e DropDownList contains 4 Items Item1 Item2 Item3 Item4 Each item when selected is bound to a corresponding page. Item1.aspx Item2.aspx Item3.aspx Item4.aspx If the default page is Item1.aspx...if I select Item3 in the DropDownList, the page Navigates to the corresponding page - Item3.aspx.  I want the DropDownList to remain displaying Item3 and not the default Item.  Thank you. for master pag...

Selecting the default selected Item in RadiobuttonList
Is it possible to set the default item selected of radiobuttonlist from code behind while  he page loads. I have been trying this with its method and properties but yet to get it right: this is the code<asp:RadioButtonList ID="rdlSetting" runat="server" RepeatColumns="2" RepeatDirection="Vertical" RepeatLayout="Table"><asp:ListItem Text="Public" Value="0"></asp:ListItem><asp:ListItem Text="Private" Value="1"></asp:ListItem></asp:RadioButtonList>from COde behindon...

How to set & show default value in dropdownlist from databound items (I am not asking the static -----select---- kind of item)
Hi I am using these: ASP.Net 2.0, VB.Net, Visual Studio 2005,  SQL Server 2005, Formview controls In a ASP.Net form I have 20 textoxes and 20 dropdownlists(ddl). All ddl(s) are databound and get the data from a single objectdatasource. All textbox and ddl support null values. Textbox is for entering a number and ddl (databound) is for selecting the unit (mm, cm, in, m3, oz, qt like these.). I am using the below code to achieve two things.(For each textbox and ddl has the corresponding codes like below.)Allowing the selection of ddl item also if it is not selected too, a default unit ite...

Set static item selected style, when one of the dynamic items is selected
Hello, I have a menu with some parent items in the static menu. Now I would like this parentItems to be gray, when I am on one of the childpages ( on of the items in the dynamicMenu ). for example:here (screenshot below ) I am on the "doelstelling"-page, which is a subitem of "Bedrijfsinfo". Now, I would like the be able to set a certain style to "Bedrijfsinfo" when a user is on the doelstelling page, or one of the other subpages.  Is this possible?  Hi,Yes this is possible, however, you have to use the CSS Friendly menu. You can find ...

Getting the Selected Item of dropdownlist inside Web User Control on a Web Form
Ive got a dropdownlist control inside a web user control called Entity.ascx. This web user control is on a webform.aspx. In Entity.ascx class I created a SelectedEntity property that returns the selected item in the dropdownlist. Inside webform.aspx when btnGo is pushed I set a string and pass it in the url. I am trying to get the entity they selected via the property (SelectedEntity) inside Entity.ascx. As the SelectedEntity property tries to return the selected item in the dropdownlist I get this error: System.NullReferenceException: Object reference not set to an instance...

Why is the select saying selected after an item is selected?
This was a whitelisting issue due to a software upgrade at hostgator..  Nothing wrong with my code..Scot King SEO Software Tool | Search Engine Optimization"Get your page rank high"  huh? It is smart to put your question in the body and not just in the title. Have no idea what your question is or if it even a question.Eric Pascarello.com | Twitter epascarello | LinkedIn use thisddl.options[ddl.selectedIndex].selected = false; //Clear the selection ddl.options[1].selected = trueMAKMark as Answer if this reply helps youMVP ASP/ASP.NetASP.Net Hosting : Host DepotMy Sit...

DropDownList Default Selected Item (using sql query)
I will try and describe what I want to do as clearly as possible.. I am using vb.net 2003 and asp.net Firstly users can have various statuses in my db Therefore if I was to execute > select status from person where persid = 10 ... it would return "Pass" When displaying this on a person details update page I wish to show a dropdownlist with all the other possible selections (obtained from an xml file called Status.xml using ReadXML method) but the default selection for this particular person to be "Pass" and not whatever the first item is to appear in the xml file This woul...

Adding an default selected item to my dropdownlist that is bound to a datatable from database
I bind relevant data to my dropdownlist, no problem upto here, however i just want not the first record of datasource appear as selectedvalue but an empty item on which writes "choose one". The items are autogenerated while binding, so is there a way to add an extra item-that will be default selected item- apart from autogenerated items. Could someone pls help me get over this? After you have bound the control, you can use the Items.Insert method e.g. MyDropDownList.Items.Insert(0, New ListItem("Choose one...",""))  Website Design Darlingt...

How to default the gridview to display all records on page_load before selecting an item from the dropdownlist...
I have successfully created a web page that can display records based on the the selection from the dropdownlist but I would like all the records to display when you first open the web page.  I am using VB.Thanks.  Your sql statement needs to use LIKE rather than = Then add add an All item to your DDL with value=%, if your DDL is databound you'll need to use AppendDataBoundItems=True Hopefully that should work, Tim I've also done something similar with the DropDownList SelectedValue as a parameter in the SQL statement, and the statement either checks for matching values or fo...

dropdownlist: select selected item during update
Hi, I have a datalist (databound) with notes ralated to persons. (Every note has one person). When I want to update a note, there is a drop downlist with al persons from the database (databound). The dropdownlist is created during the OnDataBind event of the datalist. But I want to select the previously selected person in the dropdownlist. (probably with dropdownlist.SelectedIndex). That is when the personID from the note is the same as the personID in the onDataBind of the dropdownlist. protected void dlLogboek_Ondatabind(object sender, DataListItemEventArgs e) { DropDownList ddlCpers...

How do I select an pre-selected dropdownlist item?
I have a dropdownlist for which I want to select the item previous selected, and the item id is a request.querystring item. How do I do that? dim selecteditem as string selecteditem = request.querystring(".....") dpTest.Items.FindByValue(selecteditem).Selected = True***********************Dinakar NethiLife is short. Enjoy it.*********************** Thanks !...

Adding an default selected item to my dropdownlist that is bound to a datatable from database #2
I bind relevant data to my dropdownlist, no problem upto here, however i just want not the first record of datasource appear as selectedvalue but an empty item on which writes "choose one". The items are autogenerated while binding, so is there a way to add an extra item-that will be default selected item- apart from autogenerated items. Could someone pls help me get over this?     You Insert a ListItem in the first position after you databind.  Check out this article:http://www.4guysfromrolla.com/webtech/073101-1.shtml Darrell Norton, MVPDarrell Norto...

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 = window.open('foroginkeytable.aspx','anycontent',   'width=455,height=435,status');   }  </script> &...

Setting DropDownList Selected Item from dbase SELECT
Hi! I've searched everywhere for the answer to this problem, but I can't seem to find the answer. I'm making an edit page for a user to edit their profile. One of the fields is a DropDownList containing a list of states (the states is populated from a database call to a lookup table). I want to match the state the user has in their profile to the appropriate state in the drop-down, and make that item selected. Unfortunately, I can't find any examples on how to do this. Also, since the drop-down control is within a repeater control (to populate the fields), it makes it even more diffi...

Web resources about - Dynamic DropDownList Default Selected Item Does Not Select Default Item - asp.net.web-forms

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 12/17/2015 9:29:21 PM