.NET Framework - DropDownList Binding to LINQ DataSource
Asked By JohnPadill
15-Dec-07 02: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.
LINQtoSQL
(1)
DropDownList
(1)
Page.IsPostback
(1)
LINQ
(1)
DdCustomers.DataValueField
(1)
DdCustomers.DataTextField
(1)
SelectedIndexChanged
(1)
DataBind
(1)
David R. Longnecker replied...
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

they're great - when used appropriately. They can greatly enhance the readability of code, and LINQ is a great example. Which do you think is clearer: 1) Without extension methods: IEnumerable abused. However, they allow a lot of really *nice* uses too - and enriching interfaces (as LINQ does) is probably the best use going. Of course, you can avoid using them yourself merely your opinion. Many people can and do disagree. One particular example: much of what LINQ provides would not work without extension methods. It relies on it heavily. But regardless, no of OO. Second, extension methods are the least kludgy way to work with objects in LINQ. Without extensions methods, LINQ would be both verbose and rather neutered. Used incorrectly, extension methods become a major cluster Methods? As far as I can see, a generic could have done the trick here: Linq<People> people; IEnumerable<string> names = 3D people.Where(person = 3D> person.Age > 17) .Select(person for it that I could think of. . . Still it's a wrong approach. The = same Linq syntax could have been achieved with other, more OOD, = practices. them, = 20 You got me still is. However, as I wrote Peter, it's not the usage. The usage within Linq is = fine. But implementing own Extension Methods, that's a slap in the face. = It
Is Linq to SQL any good? .NET Framework I am a big fan of linq but in my opinion linq to sql is a complete waste of time. Why on earth take something that is difficult to write by creating a translation layer. I see people here struggling to write linq to sql queries. It also creates the problem of being locked into a particular product 1) Entity Framework (1) Visual Studio (1) Silverlight (1) Oracle (1) If we ignore the LINQ to SQL vs LINQ to EF aspect. You should use LINQ to EF not LINQ to SQL. .NET wanted an ORM. Many people including me like ORM's for certain
Linq-to-Sql var null question or observation .NET Framework This is more an observation than MyMatchingTables = null, but rather I get zero hits (or .Count = 0). Must be built into Linq-to-Sql. I have been using it for years but this is the first time AField.StartsWith(aString) select X; / / why is var MyMatchingTables never null? Must be built into Linq to Sql to never return null? myMyTableList = MyMatchingTables.ToList(); } catch (Exception ex) { Debug.WriteLine(ex the point So this could return a var MyMatchingTables = 3D null? But I thought in Linq-to-Sql that is not allowed? So it will throw an exception (in the try a row out of the table encapsulated as an object / entity, as that is what Linq does. What is Language Integrated Query? LINQ is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. Microsoft LINQ defines a set of query operators that can be used to query, project and filter to the object domain * ** ** ** *). Queries written using the query operators are executed either by the LINQ query processing engine or, via an extension mechanism, handed over to LINQ providers which either
just plain text): Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then Dim dSet As DataSet Dim sqlConn As SqlConnection Dim sqlDapter As SqlDataAdapter sqlConn = New Item_Command(ByVal obj As Object, ByVal ea As RepeaterCommandEventArgs) Response.Write("Item Command<br> ") 'If (Page.IsPostBack) Then 'rptrUsers.DataBind() 'End If End Sub Sub Item_DataBound(ByVal obj As Object, ByVal ea As RepeaterItemEventArgs) Response.Write("Item DataBound<br> ") If Not (Page.IsPostBack) Then rptrUsers.DataBind() End If End Sub Sub Item_Created(ByVal obj As Object, ByVal ea doesn't get rendered. Why? Ron ASP.NET Discussions RepeaterCommandEventArgs (1) RepeaterItemEventArgs (1) StackOverflowException (1) Page.IsPostBack (1) SqlDataAdapter (1) SqlConnection (1) LinkButton (1) EventArgs (1) lUsers", Can someone please help me
the Query String is empty? .NET Framework Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim intProductID As Integer = Trim(Request("productid")) If IsNumeric(intProductID) Then drpProducts.SelectedValue = intProductID sender As Object, ByVal e As System.EventArgs) Handles Me.Load Line 19: If Not Page.IsPostBack Then Line 20: Dim intProductID As Integer = Trim(Request("productid")) How do I just have it continue without giving me the error? Thanks ASP.NET Discussions NameValueCollection (1) Page IsPostBack (1) DrpProducts.Items.FindByValue (1) System.EventArgs (1) FormatException (1) FindByValue (1) EventArgs (1) ASP Trim(Request("productid")) George. You just need to rearrange your logic a bit. . . If Not Page.IsPostBack Then If IsNumeric(Trim(Request("productid"))) Then drpProducts.SelectedValue = Trim(Request.QueryString("productid")) End