Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET 2.0/C#: Setting default selected item of a dynamically generated dropdownlist?

Posted on 2006-07-20
2
Medium Priority
?
396 Views
Last Modified: 2008-01-09
Hello experts!

I am working on web app in asp.net 2.0 and I have stumbled across a problem.

I have a page with a dropdownlist on it, which has an object data source based on the content of a MySql table.

Now, on this page, the user can select, say, an invoice from a dropdownlist.
When the user selects a dropdownlist and presses the "select" button, the 2nd dropdownlist which is also generated from a table, is filled with customers.

Basically, a user selects an invoice that he wants to edit, and then the rest of the page appears with textboxes and dropdownlists that contain the data of the invoice.

I've gotten as far as being able to fillthe textboxes with the invoice data by using:

DataSet ds = ModuleOrders.GetOrder(Order_ID);
        lblBeginDatDA.Text = ds.Tables[0].Rows[0]["Begindatum"].ToString();

ModuleOrder.GetOrder(Order_ID); is a function which gets the invoice data from the database based on the Order_ID which is tied to the first dropdownlist.

This module also returns the customer associated with the selected invoice.

Is it possible to default the 2nd dropdownlist's initially selected index to the customer field in the dataset? I am thinking of something like this:

DropDownListCustomers.InitiallySelectedValue = ds.Tables[0].Rows[0]["Customer"].ToString();

Does anyone know the correct function and syntax to set the initially selected value of a dropdownlist?

Much thanks!

Regards,

Robert
0
Comment
Question by:DarkXiphoid
[X]
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
2 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 2000 total points
ID: 17145012
you can either use
  DropDownListCustomers.SelectedValue=ds.Tables[0].Rows[0]["Customer"].ToString();

OR

ListItem item=DropDownListCustomers.Items.FindByValue(ds.Tables[0].Rows[0]["Customer"].ToString());
if (item!=null) {
  DropDownListCustomers.SelectedIndex = -1;  //Unselect currently selected item
  item.Selected=true;
} else {
  //Not found...
}
0
 
LVL 4

Author Comment

by:DarkXiphoid
ID: 17145393
Awesome,

the first method didn't work, but the second method worked!

Thanks alot.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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