DropDownList Binding to LINQ DataSource

Discussion in 'ASP .Net Web Controls' started by John Padilla, Dec 15, 2007.

  1. John Padilla

    John Padilla Guest

    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.
    John Padilla, Dec 15, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DesignerX
    Replies:
    5
    Views:
    19,655
    HemaRitchie
    Dec 29, 2008
  2. Mr Not So Know It All

    Binding multiple Datasource to DropDownlist

    Mr Not So Know It All, Jan 3, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    708
    Mr Not So Know It All
    Jan 3, 2007
  3. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    356
    Mike Gleason jr Couturier
    Nov 5, 2008
  4. Scott Rymer

    Linq Query as DataSource for all controls

    Scott Rymer, Sep 29, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    294
    Scott Rymer
    Sep 29, 2009
  5. Light
    Replies:
    0
    Views:
    821
    Light
    Oct 13, 2009
Loading...

Share This Page