Solved

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

Posted on 2006-07-20
2
394 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

617 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