DropDownList Binding to LINQ DataSource

J

John Padilla

I was playing around with the LINQtoSQL and found an interesting problem.

i am using the Northwind database on SQL 2005 Developer Server.
In my webform's PageLoad event I have the following code:

NorthwindDataContext db = new NorthwindDataContext();

var customers = from c in db.Customers
orderby c.CompanyName
select new
{
c.CompanyName,
c.CustomerID
};

ddCustomers.DataSource = customers;
ddCustomers.DataTextField = "CompanyName";
ddCustomers.DataValueField = "CustomerID";
ddCustomers.DataBind();

ddCustomers is a regular Asp.net DropDownList control.

The data populates correctly in the control at runtime. The problem occurs
when i try to select an item in the list and the control's
SelectedIntexChanged event fires .

I need the CustomerID to populate a grid from another linq query but it
doesnt matter which item in the dropdownlist i choose, once the
selectedindexevent fires the index is always set to 0 and after the postback
finishes and the page is re-rendered the dropdownlist again shows the first
Customer inthe list (index = 0)

Note when I use the LinqDataSource control in replace of my page load query
and bind to the dropdownlist control it works perfectly normal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top