LinqDataSource and ListView – a Dream Team

I just finished my first fully driven LINQ ASP.NET Page in one of my Projects by using the LINQDataSource and ListView. Let’s have a quick look at the Code.

The LINQDataSource:

<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="sqlGFDataContext" EnableDelete="True" EnableInsert="True"
EnableUpdate="True" OrderBy="commentID desc" TableName="gf_game_comments" OnDeleted="lvComments_OnDeleted">
</asp:LinqDataSource>

The ListView Control:

<asp:ListView ID="lvComments" runat="server"
DataSourceID="LinqDataSource1"
ItemContainerID="layoutTemplate"
DataKeyNames="commentID"
InsertItemPosition="None">

<Layouttemplate>
<div id="layoutTemplate" runat="server" />
</Layouttemplate>

<ItemTemplate>
Zu <%# Eval("gf_game.URL") %>"><%# Eval("gf_game.Title") %> von <%# Eval("aspnet_User.UserName") %> am <%# Eval("commentDate") %><br />
<%# Eval("Comment") %>
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="Button2" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
<AlternatingItemTemplate >
<%# Eval("UserID") %>
<%# Eval("Comment")%>
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="Button2" runat="server" CommandName="Delete" Text="Delete" />
</AlternatingItemTemplate>
<EditItemTemplate>
Comment: <asp:TextBox  runat="server" id="txtComment" Text='<%# Bind("Comment") %>'></asp:TextBox>
<br />
<asp:Button ID="Button3" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="Button4" runat="server" CommandName="Cancel" Text="Cancel" /><br />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox runat="server" ID="txtUserID" Text='<%# Bind("GameID") %>'></asp:TextBox>
<asp:TextBox  runat="server" id="txtAbstract" Text='<%# Bind("Comment") %>'></asp:TextBox>
<asp:Button ID="Button3" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="Button4" runat="server"
CommandName="Cancel" Text="Cancel" /><br />
</InsertItemTemplate>
</asp:ListView>

And if you need to do some custom operations while the Delete Event is fireing you can hook it up really easy:

protected void lvComments_OnDeleted(Object sender, LinqDataSourceStatusEventArgs e)
{
var gc = (gf_game_comment)e.Result;
csFN.ReCalcUserFactor(Int32.Parse(gc.GameID.ToString()));
}

As you see you got access to the complete table object with all its data inside, it’s really comfortable.

All this stuff is almost working out of the box if you use the new LinqDataSource Control and enable support for Inserting, Updating and Deleting Data while binding the DataSource. It’s really amazing how easy this stuff got, you don’t even have to care about JOINS, LINQ is doing everything for you.

If you check the code on my ListView again you notice this Eval Code – Eval(“aspnet_User.UserName”) – I got access to this tablefield without writing any SQL Join. LINQ automatically checks the relations between your tables and sorts out the specific data. This could be another great step in productivity as you don’t have to take care of huge SQL Querystrings anymore.

More on this topic:
Scott Gu for setting up the ContextType (required for the LINQ DataSource): Binding UI using a LINQ DataSource
Rick Strahl for a quick Overview on the new Controls by using a SQLDataSource: ListView and DataPager in ASP.NET 3.5.

4 comments ↓

#1 Anne Ramos on 08.09.07 at 4:57 pm

🙂 Is it possible for you to send me a zipped file containing a runable demo of your code. – I would appreciate it

#2 NagiReddy on 02.20.08 at 11:53 am

Is it possible for you to send me a zipped file containing a runable demo of your code. – I would appreciate it

#3 BenP on 04.18.08 at 4:33 pm

Can you tell me if you have got updating to work when binding to a linqdatasource?

I notice you haven’t disabled the view state in your demo, so you may be unaware of the problem. With view state enabled, updating seems to work. Then I discovered that the changes don’t go to the database. With viewstate off it just bombs complaining about an existing entity with the same key (well duh, it is an update!)

Can you confirm that yours actually works?

Thanks

#4 zZ on 12.12.08 at 1:14 pm

ZZ

Leave a Comment