Can't get rid of this error. ListView. Please, need help.

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"/
in configuration or <%@ Page EnableEventValidation="true" %> in a
page.
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">
<asp:placeHolder ID="phListViewLab" runat="server"></
asp:placeHolder>
</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 = "&nbsp;"
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
 
J

John Kotuby

Have you tried including EnableEventValidation="false" in your @Page
directive?

As in:

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

It's not really recommended for good security practices but it might get you
a fix if you are sure the data is valid.
 
S

shapper

Have you tried including EnableEventValidation="false" in your @Page
directive?

As in:

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

It's not really recommended for good security practices but it might get you
a fix if you are sure the data is valid.

Hi,

I tried that and I stoped getting that error but then my ListView does
not work.
But I really need to solve my problem ... and find why I am having it.

I can't fix it using something that is not advisible and not knowing
why.
In the code I posted I do not have any custom control ... only
standard ASP.NET controls.
I can't see why do I get this problem.

Thanks,
Miguel
 
S

shapper

Hi,

I tried that and I stoped getting that error but then my ListView does
not work.
But I really need to solve my problem ... and find why I am having it.

I can't fix it using something that is not advisible and not knowing
why.
In the code I posted I do not have any custom control ... only
standard ASP.NET controls.
I can't see why do I get this problem.

Thanks,
Miguel

Hello,

I found the problem!

If I replace the Cancel and Update buttons by LinkButtons everything
is solved.

Now the question: why does this happens? Can't figure this out.

Any idea? Anyone?

Thanks,

Miguel
 
J

John Kotuby

Miguel,
May I suggest that you read up on Validation of user input to avoid
malicious attacks on your website.
Here is a link to an older article that provides a very straight-forward
explanation of the reasons for both Server-side and Client-side validation.
You will find additional links on that page with in-depth discussions about
cross-site scripting and other malicious attacks.

http://aspalliance.com/22

Different controls handle validation differently. LinkButtons use Javascript
to accomplish page postbacks.

I know that the MSDN Help can be difficult reading at best. But please try
doing a bit more research before posting. Even Googling some of the terms in
error messages or articles you don't understand can give you most of the
answers you seek.

Respectfully...
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top