HighTechTalks DotNet Forums  

DropDownList Binding to LINQ DataSource

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss DropDownList Binding to LINQ DataSource in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
John Padilla
 
Posts: n/a

Default DropDownList Binding to LINQ DataSource - 12-15-2007 , 01:08 PM






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.

Reply With Quote
  #2  
Old   
David R. Longnecker
 
Posts: n/a

Default Re: DropDownList Binding to LINQ DataSource - 12-19-2007 , 08:57 AM






John-

Would it be beneficial to surround your data fetching code in Page_Load()
with:

if (!Page.IsPostback)
{

// code

}

Your code is refetching and rebinding your data each time--to the default
values. Page_Load fires on each postback so, when your SelectedIndexChanged
fires and the page posts back, the data source resets.

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

Quote:
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.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.