S
shapper
Hello,
I created an ASP.NET ListView at runtime connected to a
LinqDataSource. I always get the same error when I click the Cancel or
Update buttons in EditItemTemplate:
Invalid postback or callback argument.
Event validation is enabled using <pages enableEventValidation="true"/
For security purposes, this feature verifies that arguments to
postback or callback events originate from the server control that
originally rendered them.
If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
I tried everything I could think of for the past 2 days but I can't
figure out what is wrong.
Could someone, please, help me?
I am posting my entire code.
Thank You,
Miguel
ListViewLab.Aspx
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="ListViewLab.aspx.vb" Inherits="ListViewLab" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="fListViewLab" runat="server">
<asplaceHolder ID="phListViewLab" runat="server"></
asplaceHolder>
</form>
</body>
</html>
ListViewLab.Aspx.vb
Partial Class ListViewLab
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
phListViewLab.Controls.Add(ldsTags)
phListViewLab.Controls.Add(lvTags)
End Sub
Private Sub ldsTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ldsTags.Init
With ldsTags
.ContextTypeName = "MyListViewDataContext"
.EnableDelete = True
.EnableInsert = True
.EnableUpdate = True
.TableName = "Tags"
.ID = "ldsTags"
End With
End Sub
Private Sub ldsTags_Selecting(ByVal sender As Object, ByVal e As
LinqDataSourceSelectEventArgs) Handles ldsTags.Selecting
Dim database As New MyListViewDataContext
Dim tags = From t In database.Tags _
Select t.TagID, _
t.Text, _
Active = t.FilesTags.Any Or t.ArticlesTags.Any
e.Result = tags
End Sub
Private Sub lvTags_ItemDeleting(ByVal sender As Object, ByVal e As
ListViewDeleteEventArgs) Handles lvTags.ItemDeleting
Dim database As New MyListViewDataContext
Dim tag = (From t In database.Tags _
Where t.TagID = New
Guid(lvTags.DataKeys(e.ItemIndex).Value.ToString)).Single
database.Tags.DeleteOnSubmit(tag)
database.SubmitChanges()
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
.EditItemTemplate = New
ListViewLabTemplate(TemplateType.EditItemTemplate)
.ItemTemplate = New
ListViewLabTemplate(TemplateType.ItemTemplate)
.LayoutTemplate = New
ListViewLabTemplate(TemplateType.LayoutTemplate)
End With
End Sub
Private Sub lvTags_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles lvTags.Load
lvTags.DataBind()
End Sub
ListViewTemplate Class
Public Class ListViewLabTemplate
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 ' Type
Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Select Case Me.Type
Case TemplateType.EditItemTemplate
EditItemTemplate(container)
Case TemplateType.ItemTemplate
ItemTemplate(container)
Case TemplateType.LayoutTemplate
LayoutTemplate(container)
End Select
End Sub ' InstantiateIn
Private Sub lbDelete_Init(ByVal sender As Object, ByVal e As
EventArgs)
Dim lbDelete As LinkButton = CType(sender, LinkButton)
With lbDelete
.CommandName = "Delete"
.ID = "lbDelete"
.Text = "Delete"
End With
End Sub
Private Sub lbEdit_Init(ByVal sender As Object, ByVal e As
EventArgs)
Dim lbEdit As LinkButton = CType(sender, LinkButton)
With lbEdit
.CommandName = "Edit"
.ID = "lbEdit"
.Text = "Edit"
End With
End Sub
Private Sub lText_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim lText As Literal = CType(sender, Literal)
Dim lvdiContainer As ListViewDataItem =
CType(lText.NamingContainer, ListViewDataItem)
lText.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub
Public Sub New(ByVal type As TemplateType)
Me.Type = type
End Sub
Private Sub EditItemTemplate(ByVal container As Control)
Dim form As New HtmlTableCell
form.ColSpan = 3
Dim a As New Button
a.CommandName = "Cancel"
a.CommandArgument = ""
a.Text = "Cancel"
Dim b As New Button
b.CommandName = "Update"
b.CommandArgument = ""
b.Text = "Update"
Dim c As New TextBox
c.ID = "tbTag"
AddHandler c.DataBinding, AddressOf c_DataBinding
form.Controls.Add(a)
form.Controls.Add(b)
form.Controls.Add(c)
Dim tag As New HtmlTableRow
tag.Controls.Add(form)
container.Controls.Add(tag)
End Sub
Private Sub ItemTemplate(ByVal container As Control)
Dim lbDelete As New LinkButton
AddHandler lbDelete.Init, AddressOf lbDelete_Init
Dim lbEdit As New LinkButton
AddHandler lbEdit.Init, AddressOf lbEdit_Init
Dim lText As New Literal
AddHandler lText.DataBinding, AddressOf lText_DataBinding
Dim command As New HtmlTableCell
command.Attributes.Add("class", "Command")
With command.Controls
.Add(lbEdit)
.Add(lbDelete)
End With
Dim text As New HtmlTableCell
text.Controls.Add(lText)
Dim tag As New HtmlTableRow
tag.Cells.Add(text)
tag.Cells.Add(command)
container.Controls.Add(tag)
End Sub
Private Sub LayoutTemplate(ByVal container As Control)
Dim command As New HtmlTableCell("th")
command.Attributes.Add("class", "Command")
command.InnerHtml = " "
Dim text As New HtmlTableCell("th")
text.InnerHtml = "Tag"
Dim fields As New HtmlTableRow
With fields.Cells
.Add(text)
.Add(command)
End With
Dim body As New HtmlGenericControl("tbody")
body.ID = "itemPlaceholder"
Dim table As New HtmlGenericControl("table")
table.ID = "tTags"
table.Controls.Add(body)
End Sub
Private Sub c_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim c As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem = CType(c.NamingContainer,
ListViewDataItem)
c.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub
End Class
I created an ASP.NET ListView at runtime connected to a
LinqDataSource. I always get the same error when I click the Cancel or
Update buttons in EditItemTemplate:
Invalid postback or callback argument.
Event validation is enabled using <pages enableEventValidation="true"/
page.in configuration or <%@ Page EnableEventValidation="true" %> in a
For security purposes, this feature verifies that arguments to
postback or callback events originate from the server control that
originally rendered them.
If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
I tried everything I could think of for the past 2 days but I can't
figure out what is wrong.
Could someone, please, help me?
I am posting my entire code.
Thank You,
Miguel
ListViewLab.Aspx
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="ListViewLab.aspx.vb" Inherits="ListViewLab" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="fListViewLab" runat="server">
<asplaceHolder ID="phListViewLab" runat="server"></
asplaceHolder>
</form>
</body>
</html>
ListViewLab.Aspx.vb
Partial Class ListViewLab
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
phListViewLab.Controls.Add(ldsTags)
phListViewLab.Controls.Add(lvTags)
End Sub
Private Sub ldsTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ldsTags.Init
With ldsTags
.ContextTypeName = "MyListViewDataContext"
.EnableDelete = True
.EnableInsert = True
.EnableUpdate = True
.TableName = "Tags"
.ID = "ldsTags"
End With
End Sub
Private Sub ldsTags_Selecting(ByVal sender As Object, ByVal e As
LinqDataSourceSelectEventArgs) Handles ldsTags.Selecting
Dim database As New MyListViewDataContext
Dim tags = From t In database.Tags _
Select t.TagID, _
t.Text, _
Active = t.FilesTags.Any Or t.ArticlesTags.Any
e.Result = tags
End Sub
Private Sub lvTags_ItemDeleting(ByVal sender As Object, ByVal e As
ListViewDeleteEventArgs) Handles lvTags.ItemDeleting
Dim database As New MyListViewDataContext
Dim tag = (From t In database.Tags _
Where t.TagID = New
Guid(lvTags.DataKeys(e.ItemIndex).Value.ToString)).Single
database.Tags.DeleteOnSubmit(tag)
database.SubmitChanges()
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
.EditItemTemplate = New
ListViewLabTemplate(TemplateType.EditItemTemplate)
.ItemTemplate = New
ListViewLabTemplate(TemplateType.ItemTemplate)
.LayoutTemplate = New
ListViewLabTemplate(TemplateType.LayoutTemplate)
End With
End Sub
Private Sub lvTags_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles lvTags.Load
lvTags.DataBind()
End Sub
ListViewTemplate Class
Public Class ListViewLabTemplate
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 ' Type
Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Select Case Me.Type
Case TemplateType.EditItemTemplate
EditItemTemplate(container)
Case TemplateType.ItemTemplate
ItemTemplate(container)
Case TemplateType.LayoutTemplate
LayoutTemplate(container)
End Select
End Sub ' InstantiateIn
Private Sub lbDelete_Init(ByVal sender As Object, ByVal e As
EventArgs)
Dim lbDelete As LinkButton = CType(sender, LinkButton)
With lbDelete
.CommandName = "Delete"
.ID = "lbDelete"
.Text = "Delete"
End With
End Sub
Private Sub lbEdit_Init(ByVal sender As Object, ByVal e As
EventArgs)
Dim lbEdit As LinkButton = CType(sender, LinkButton)
With lbEdit
.CommandName = "Edit"
.ID = "lbEdit"
.Text = "Edit"
End With
End Sub
Private Sub lText_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim lText As Literal = CType(sender, Literal)
Dim lvdiContainer As ListViewDataItem =
CType(lText.NamingContainer, ListViewDataItem)
lText.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub
Public Sub New(ByVal type As TemplateType)
Me.Type = type
End Sub
Private Sub EditItemTemplate(ByVal container As Control)
Dim form As New HtmlTableCell
form.ColSpan = 3
Dim a As New Button
a.CommandName = "Cancel"
a.CommandArgument = ""
a.Text = "Cancel"
Dim b As New Button
b.CommandName = "Update"
b.CommandArgument = ""
b.Text = "Update"
Dim c As New TextBox
c.ID = "tbTag"
AddHandler c.DataBinding, AddressOf c_DataBinding
form.Controls.Add(a)
form.Controls.Add(b)
form.Controls.Add(c)
Dim tag As New HtmlTableRow
tag.Controls.Add(form)
container.Controls.Add(tag)
End Sub
Private Sub ItemTemplate(ByVal container As Control)
Dim lbDelete As New LinkButton
AddHandler lbDelete.Init, AddressOf lbDelete_Init
Dim lbEdit As New LinkButton
AddHandler lbEdit.Init, AddressOf lbEdit_Init
Dim lText As New Literal
AddHandler lText.DataBinding, AddressOf lText_DataBinding
Dim command As New HtmlTableCell
command.Attributes.Add("class", "Command")
With command.Controls
.Add(lbEdit)
.Add(lbDelete)
End With
Dim text As New HtmlTableCell
text.Controls.Add(lText)
Dim tag As New HtmlTableRow
tag.Cells.Add(text)
tag.Cells.Add(command)
container.Controls.Add(tag)
End Sub
Private Sub LayoutTemplate(ByVal container As Control)
Dim command As New HtmlTableCell("th")
command.Attributes.Add("class", "Command")
command.InnerHtml = " "
Dim text As New HtmlTableCell("th")
text.InnerHtml = "Tag"
Dim fields As New HtmlTableRow
With fields.Cells
.Add(text)
.Add(command)
End With
Dim body As New HtmlGenericControl("tbody")
body.ID = "itemPlaceholder"
Dim table As New HtmlGenericControl("table")
table.ID = "tTags"
table.Controls.Add(body)
End Sub
Private Sub c_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim c As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem = CType(c.NamingContainer,
ListViewDataItem)
c.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub
End Class