Linq and ListView

TJNevis

New member
Joined
Sep 22, 2008
Messages
2
Programming Experience
Beginner
Hello all. I am trying to figure out the ListView control and Linq. The reason I am using Linq is because from what I read about it, it seems easy to write code (I use VB.NET) to conditionally execute statements since the DataContext strongly types the database and its relationships; let me know if I am understanding this wrong.

The relationship for the database is the greenId. I am not sure if I set up the database in an efficient and well structured way, but that is another topic. Any advice or articles to read would be much appreciated!

databaseSetup.jpg

Database Setup (DBML)

So far I have code that creates an instance of the DataContext and pulls everything back in a variable (licenseData)



VB.NET:
1    Dim db As New EcozineDataContext
2    
3    Dim licenseData = From p In db.GreenUserLicenses Select p
4    
5    ListView1.DataSource = licenseData
6    ListView1.DataBind()


I wrote, what seems to be pretty much blank, methods - which I saw in this post (ListView ... Update and Insert - ASP.NET Forums)

I have been watching the LINQ videos on ASP.NET (LINQ Videos : The Official Microsoft ASP.NET Site) - but the database is updated manually. I need the database to be updated from what the user types in a textbox, dynamically.

Also, I have been trying to wrap my head around the difference between LINQ, such as the code I used above, and LinqDatasource...is that what I am missing to be able to update/delete/insert? Do I need a LinqDataSource to go together with LINQ code to be able to update/delete/insert?

This is my complete code. I have the ListView populating nothing to begin with, only when you click the All button, so I could change the ListView by clicking the All button or other buttons I was planning on putting in.



userTest.aspx

HTML:
1    <%@ Page Title="" Language="VB" MasterPageFile="~/green/MasterPages/UserMP.master" AutoEventWireup="false" CodeFile="userTest.aspx.vb" Inherits="green_user_userTest" %>
2    
3    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
4    </asp:Content>
5    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
6    <asp:listview id="ListView1" runat="server" style="margin-right: 0px" DataKeyNames="licenseDataId">
7            <LayoutTemplate>
8                            <table bgcolor="White">
9                                <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
10                           </table>
11                       </LayoutTemplate>
12                       <itemTemplate>
13                           <%#Eval("State")%><br />
14                           <%#Eval("licenseClass")%><br />
15                           <%#Eval("licenseType")%><br />
16                           <%#Eval("issueDate")%><br />
17                           <%#Eval("renewalDate")%><br /><br />
18                           <asp:LinkButton ID="LinkButton1" runat="server" Text="Edit" CommandName="Edit" /><br /><br />
19                       </itemTemplate>
20                       <EditItemTemplate>
21                           <asp:TextBox ID="licenseDataId" runat="server" Text='<%#Bind("licenseDataId")%>' /><br />
22                           <asp:TextBox ID="State" runat="server" Text='<%#Bind("State")%>' /><br />
23                           <asp:TextBox ID="licenseClass" runat="server" Text='<%#Bind("licenseClass")%>' /><br />
24                           <asp:TextBox ID="licenseType" runat="server" Text='<%#Bind("licenseType")%>' /><br />
25                           <asp:TextBox ID="issueDate" runat="server" Text='<%#Bind("issueDate")%>' /><br />
26                           <asp:TextBox ID="renewalDate" runat="server" Text='<%#Bind("renewalDate")%>' /><br /><br />
27                           <asp:LinkButton ID="UpdateButton" runat="Server" Text="Update" CommandName="Update" />
28                       </EditItemTemplate>
29       </asp:listview>
30       <asp:datapager id="DataPager1" runat="server" pagedcontrolid="ListView1">
31           <Fields>
32               <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
33                   ShowLastPageButton="True" />
34           </Fields>
35       </asp:datapager>
36       <br />
37       <br />
38   
39   
40       <asp:button id="Button1" runat="server" text="All" style="height: 26px" />
41       <asp:button id="Button2" runat="server" 
42           text="Only Person with licenseDataId of 2" Width="310px" />
43       <br />
44       <br />
45   </asp:Content>

userTest.aspx.vb



VB.NET:
1    Imports System
2    Imports System.Linq
3    Partial Class green_user_userTest
4        Inherits System.Web.UI.Page
5        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
6            Dim db As New EcozineDataContext
7    
8            Dim licenseData = From p In db.GreenUserLicenses Select p
9    
10           ListView1.DataSource = licenseData
11           ListView1.DataBind()
12       End Sub
13   
14       Private Sub BindListView()
15           Dim db As New EcozineDataContext
16   
17           Dim licenseData = From p In db.GreenUserLicenses Select p
18   
19           ListView1.DataSource = licenseData
20           ListView1.DataBind()
21   
22   
23       End Sub
24   
25       Protected Sub ListView1_ItemCanceling(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCancelEventArgs) Handles ListView1.ItemCanceling
26           ListView1.EditIndex = -1
27           BindListView()
28   
29       End Sub
30   
31       Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
32           If e.CommandName = "Edit" Then
33   
34   
35           End If
36       End Sub
37   
38       Protected Sub ListView1_ItemEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewEditEventArgs) Handles ListView1.ItemEditing
39           ListView1.EditIndex = e.NewEditIndex
40           BindListView()
41   
42       End Sub
43   
44       Protected Sub ListView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles ListView1.ItemUpdating
45           'Override internal databound method
46           ' call your own updating code here
47           '
48           e.Cancel = True
49           ListView1.EditIndex = -1
50           BindListView()
51   
52       End Sub
53   
54       Protected Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
55   
56       End Sub
57   
58   End Class
 
Back
Top