Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now