ListView ... Update and Insert

S

shapper

Hello,

I created a ListView with LinqDataSource and DataPager at Runtime.

Everything is working but when I Insert a record I get the following
error:

LinqDataSource 'ldsTags' has no values to insert. Check that the
'values' dictionary contains values.

And when I update a record the changes are not saved.

I think the problem is the same but I am not sure how to solve this.

What am I doing wrong?

I am posting all my code.

Thanks,

Miguel

ASPX

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="ListViewRuntime.aspx.vb"
Inherits="ListViewRuntime" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>List View Runtime</title>
</head>
<body>
<form id="fListViewRuntime" runat="server">
<asp:placeHolder ID="phListViewRuntime" runat="server"></
asp:placeHolder>
</form>
</body>
</html>

ASPX.VB

Public Enum TemplateType
AlternatingItemTemplate
EditItemTemplate
EmptyDataTemplate
EmptyItemTemplate
GroupSeparatorTemplate
GroupTemplate
InsertItemTemplate
ItemSeparatorTemplate
ItemTemplate
LayoutTemplate
SelectedItemTemplate
End Enum

Partial Class ListViewRuntime
Inherits System.Web.UI.Page

Protected WithEvents ldsTags As New LinqDataSource
Protected WithEvents lvTags As New ListView

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Init

phListViewRuntime.Controls.Add(ldsTags)
phListViewRuntime.Controls.Add(lvTags)

End Sub

Private Sub ldsTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ldsTags.Init

With ldsTags
.ContextTypeName = "CodeDataContext"
.EnableDelete = True
.EnableInsert = True
.EnableUpdate = True
.ID = "ldsTags"
.TableName = "Tags"
End With

End Sub

Private Sub lvTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles lvTags.Init

With lvTags
.DataKeyNames = New String() {"TagID"}
.DataSourceID = "ldsTags"
.ID = "lvTags"
.InsertItemPosition = InsertItemPosition.FirstItem
End With

With lvTags
.AlternatingItemTemplate = New
Template(TemplateType.AlternatingItemTemplate)
.LayoutTemplate = New Template(TemplateType.LayoutTemplate)
.InsertItemTemplate = New
Template(TemplateType.InsertItemTemplate)
.SelectedItemTemplate = New
Template(TemplateType.SelectedItemTemplate)
.EmptyDataTemplate = New
Template(TemplateType.EmptyDataTemplate)
.EditItemTemplate = New Template(TemplateType.EditItemTemplate)
.ItemTemplate = New Template(TemplateType.ItemTemplate)
End With

End Sub

End Class


Public Class Template
Implements ITemplate

Private _Type As TemplateType
Public Property Type() As TemplateType
Get
Return _Type
End Get
Set(ByVal value As TemplateType)
_Type = value
End Set
End Property

Public Sub New(ByVal type As TemplateType)
Me.Type = type
End Sub

Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn

' Select template type
Select Case Me.Type

Case TemplateType.AlternatingItemTemplate
ItemTemplate(container)

Case TemplateType.LayoutTemplate
LayoutTemplate(container)

Case TemplateType.ItemTemplate
ItemTemplate(container)

Case TemplateType.SelectedItemTemplate
ItemTemplate(container)

Case TemplateType.EmptyDataTemplate

Case TemplateType.EditItemTemplate
EditItemTemplate(container)

Case TemplateType.InsertItemTemplate
InsertItemTemplate(container)

End Select

End Sub

Private Sub LayoutTemplate(ByVal container As Control)

Dim table As New HtmlGenericControl("table")

Dim header As New HtmlGenericControl("thead")
table.Controls.Add(header)

Dim headerrow As New HtmlGenericControl("tr")
header.Controls.Add(headerrow)

Dim text As New HtmlTableCell("th")
text.InnerHtml = "Tag"
headerrow.Controls.Add(text)

Dim command As New HtmlTableCell("th")
command.InnerHtml = "Command"
headerrow.Controls.Add(command)

Dim body As New HtmlGenericControl("tbody")
body.ID = "itemPlaceholder"
table.Controls.Add(body)

Dim footer As New HtmlGenericControl("tfoot")
table.Controls.Add(footer)

Dim footerrow As New HtmlGenericControl("tr")
footer.Controls.Add(footerrow)

Dim pagercell As New HtmlTableCell("th")
pagercell.ColSpan = 2
footerrow.Controls.Add(pagercell)

Dim pager As New DataPager
footerrow.Controls.Add(pager)

Dim field As New NextPreviousPagerField
With field
.ButtonType = ButtonType.Button
.ShowFirstPageButton = True
.ShowLastPageButton = True
End With
pager.Fields.Add(field)

container.Controls.Add(table)

End Sub

Private Sub ItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim delete As New Button
delete.ID = "delete"
delete.Text = "Delete"
delete.CommandName = "Delete"
command.Controls.Add(delete)

Dim edit As New Button
edit.ID = "edit"
edit.Text = "Edit"
edit.CommandName = "Edit"
command.Controls.Add(edit)

Dim text As New Literal
text.ID = "text"
tag.Controls.Add(text)
AddHandler text.DataBinding, AddressOf text_DataBinding

container.Controls.Add(item)

End Sub

Private Sub InsertItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim insert As New Button
insert.ID = "insert"
insert.Text = "Insert"
insert.CommandName = "Insert"
command.Controls.Add(insert)

Dim cancel As New Button
cancel.ID = "cancel"
cancel.Text = "Cancel"
cancel.CommandName = "Cancel"
command.Controls.Add(cancel)

Dim inserttext As New TextBox
inserttext.ID = "inserttext"
tag.Controls.Add(inserttext)
AddHandler inserttext.DataBinding, AddressOf
inserttext_DataBinding

container.Controls.Add(item)

End Sub

Private Sub EditItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim update As New Button
update.ID = "update"
update.Text = "Update"
update.CommandName = "Update"
command.Controls.Add(update)

Dim cancel As New Button
cancel.ID = "cancel"
cancel.Text = "Cancel"
cancel.CommandName = "Cancel"
command.Controls.Add(cancel)

Dim updatetext As New TextBox
updatetext.ID = "updatetext"
tag.Controls.Add(updatetext)
AddHandler updatetext.DataBinding, AddressOf
updatetext_DataBinding

container.Controls.Add(item)

End Sub

Private Sub text_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim text As Literal = CType(sender, Literal)
Dim lvdiContainer As ListViewDataItem =
CType(text.NamingContainer, ListViewDataItem)
text.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

Private Sub inserttext_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim inserttext As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem =
CType(inserttext.NamingContainer, ListViewDataItem)
inserttext.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

Private Sub updatetext_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim updatetext As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem =
CType(updatetext.NamingContainer, ListViewDataItem)
updatetext.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

End Class
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top