Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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