473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dropdown lists in asp.net

I am trying to select the item in the dropdown list the matches a value
pulled from the database and set this matching item's selected property to
true so that it is the selected item in the combo box(dropdown list). How
would I do this in C#? It seems like it should be real easy but I can't make
it work. Thanks in advance

Nov 16 '05 #1
1 1353

if you're database value to match against is stored as a string, say:
string myDatabaseValue;

and your dropdownlist is defined as something like:
System.Web.UI.WebControls.DropDownList myDropdownList;

then you'd do the following in the Page_Load(...) function

private void Page_Load(object sender, System.EventArgs e)
{
if ( !Page.IsPostBack )
{
//
// code here to populate the myDatabaseValue string

//
// code here to populate the myDropdownList
programmatically

if ( myDropdownList.Items.Count > 0 )
{
try
{
myDropdownList.SelectedValue =
myDatabaseValue;
}
catch ( Exception )
{
myDropdownList.SelectedValue = 0;
}
}
} // endof !Page Postback
} // end of Page_Load
Hope that helps...
For an example on MSDN see:
http://msdn.microsoft.com/library/de...valuetopic.asp
(beware of line wraps)

"Lance" <La***@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
I am trying to select the item in the dropdown list the matches a value
pulled from the database and set this matching item's selected property to
true so that it is the selected item in the combo box(dropdown list). How
would I do this in C#? It seems like it should be real easy but I can't
make
it work. Thanks in advance

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Toonman | last post by:
I have a webpage with a <form> consisting of a large table grid of dropdown lists used to make changes in a database. Some of these dropdown lists have the same value. I'm trying to make it so...
0
by: Mike O. | last post by:
MS Access 2003 "filter by form" has drop down lists that allow the user to select values for each field to filter by. However, once some values are selected,the remaining dropdown lists remain the...
0
by: E . | last post by:
I seem to be getting nowhere with this problem here is the situation. I have a webform with a data grid populated with the results of a table that has only 3 fields. Then below that are 1...
0
by: Henke | last post by:
Hi, I have done some research about my problem I have when using the "back button" in IE to go back to a page with two dropdown lists. The both dropdown lists are populated with data. The...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
1
by: cgian31 | last post by:
I have implemented a website where in a page are represented the data of one record, using cascade dropdown lists in 2 levels, within the same page/record. According to the selection in the...
3
by: er1 | last post by:
Hi all, I have created a double dropdown list. Based on the first list selection, second list populates (this works fine). I have a submit button, which when clicked should run a select query...
4
by: Paul | last post by:
Hi all, I have a page that has a form on it which has a dropdown list on it. It connect to an sql database and populate the list. What I would like to do is make the list editable so that if the...
3
by: John | last post by:
I have two dropdown lists that I have bound to a datatable and set the DataTextField and DataValueField for. Both lists show the values I expect from the database. However, when I need to access...
5
by: plumba | last post by:
Hi all. I have two drop down menus, the first a list of Departments, the second a list of Sections. Each Department has a set of Setions, so the Sections dropdown contains complete list of all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.