Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-20
2
Medium Priority
?
399 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
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

773 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