J
jason.neo
Hi all experts,
I am going nuts with this Invalid postback or callback argument thingy
with .Net 2.0
I am building a file attachment module which relays on a Datatable
stored in session (yeah i know its unhealthy, but its the only "clean"
approach which I can think of to avoid tranfering temp files onto the
server) to maintain the list of attached files before the "commit"
action is done to transfer all the files onto the server.
I have a function which allows the user to delete the attached file
before the "commit" action is performed. Essentially, the "delete" is
deleting a row from the Datatable stored in session. When I was doing
testing of this code in the early phase, all works well, but suddenly a
few days later, when I improved the codes to incorporate other
functions, whenever I click on the "delete" button (which is a template
column with an image button that triggers a "RowCommand" to handle the
deletion) i will hit the Invalid postback error. I have since removed
those new codes but I am still stuck with the problem.
Someone help me please! I am totally clueless as how to resolve this
issue. I have tried to turn off the EnableEventValidation at the page
level, but when I do so, my "RowCommand" event is not being triggered
(it works fine if i turn on the validation). You guys see the
fustration I am facing here?
Would really really be glad if someone could help me. I have attached
my codes below so you guys can help me take a look. Do let me know if
you need more information.
------------- Code Behind ---------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindAttachmentGrid(True)
Else
BindAttachmentGrid(False)
End If
vldInvalidFile.Visible = False
vldLimitExceeded.Visible = False
vldDuplicate.Visible = False
End Sub
Private Sub BindAttachmentGrid(ByVal blnBindEmpty As Boolean)
Dim dt As DataTable
If blnBindEmpty = False Then
dt = Me.p_AttachmentTempStorage <-- "this
p_AttachmentTempStorage is a private property which will determine if
an existing DT is stored in the session, if not, it will call the
CreateAttachmentDT" -->
End If
gdvAttachment.DataSource = dt
gdvAttachment.DataBind()
End Sub
Private Sub CreateAttachmentDT()
Dim dtAttach As New DataTable
dtAttach.Columns.Add("ID", GetType(Integer))
dtAttach.Columns.Add("FileName", GetType(String))
dtAttach.Columns.Add("RawFileSize", GetType(Integer))
dtAttach.Columns.Add("FileSize", GetType(String))
dtAttach.Columns.Add("UploadedBy", GetType(String))
dtAttach.Columns.Add("UploadedDateTime", GetType(String))
dtAttach.Columns.Add("FileStream",
GetType(WebControls.FileUpload))
dtAttach.Columns.Add("FileTypeFlag", GetType(String))
dtAttach.Columns.Add("FilePath", GetType(String))
dtAttach.PrimaryKey = New DataColumn() {dtAttach.Columns("ID")}
Me.p_AttachmentTempStorage = dtAttach
End Sub
Protected Sub btnAttach_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAttach.Click
If fileAttach.HasFile = True Then
Dim tmpTotalSize As Double = Me.p_TotalUploadedSize <--
"all Me.p_XXXX are simply private property which stores information in
viewstate" -->
tmpTotalSize += fileAttach.PostedFile.ContentLength
If tmpTotalSize > 10485760 Then
vldLimitExceeded.Visible = True
Exit Sub
End If
If Me.p_AllowDuplication = False Then
For Each dRowItem As DataRow In
Me.p_AttachmentTempStorage.Rows
If dRowItem("FileName").ToString.Trim =
fileAttach.FileName Then
vldDuplicate.Visible = True
Exit Sub
End If
Next
End If
Dim dRow As DataRow
Me.p_TotalUploadedSize +=
fileAttach.PostedFile.ContentLength
dRow = Me.p_AttachmentTempStorage.NewRow()
Me.p_AttachmentID += 1
dRow("ID") = Me.p_AttachmentID
dRow("FileName") = fileAttach.FileName
dRow("RawFileSize") = fileAttach.PostedFile.ContentLength
dRow("FileSize") =
ConvertFileSize(fileAttach.PostedFile.ContentLength)
dRow("UploadedBy") =
clsUser.getUserName(clsUser.getEmployeeIDByLoginID(MyBase.GetLogonUser()))
dRow("UploadedDateTime") = Format(Now,
clsUtil.ConfigValue("dateTimeFormat"))
dRow("FileStream") = fileAttach
dRow("FileTypeFlag") = "New"
dRow("FilePath") = ""
Me.p_AttachmentTempStorage.Rows.Add(dRow)
Me.p_AttachmentTempStorage.AcceptChanges()
If gdvAttachment.PageCount <> 0 Then
gdvAttachment.PageIndex = gdvAttachment.PageCount
End If
BindAttachmentGrid(False)
Else
vldInvalidFile.Visible = True
End If
End Sub
Protected Sub gdvAttachment_RowDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
gdvAttachment.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(2).Text = "Size <br>(" &
ConvertFileSize(Me.p_TotalUploadedSize) & " Total)"
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
Dim lBtnFileName As LinkButton =
CType(e.Row.FindControl("lBtnFileName"), LinkButton)
lBtnFileName.CommandArgument = e.Row.DataItem("ID")
AddHandler lBtnFileName.Click, AddressOf lBtnFileName_Click
CType(e.Row.FindControl("btnAttachmentDelete"),
ImageButton).Attributes.Add("onclick", "return confirm('Are you sure
you want to delete " & e.Row.DataItem("FileName") & "?')")
CType(e.Row.FindControl("btnAttachmentDelete"),
ImageButton).CommandArgument = e.Row.DataItem("ID")
End If
End Sub
<-- "The following code suppose to be the one to handle the delete
event, but before this code is even run, I hit the above error
already!" -->
Protected Sub gdvAttachment_RowCommand(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
gdvAttachment.RowCommand
If e.CommandName = "CustomDelete" Then
Try
Dim dRow As DataRow =
Me.p_AttachmentTempStorage.Rows.Find(e.CommandArgument)
If dRow("FileTypeFlag") = "Existing" Then
If System.IO.File.Exists(dRow("FilePath")) = True
Then
System.IO.File.Delete(dRow("FilePath"))
End If
End If
Me.p_TotalUploadedSize -= dRow("RawFileSize")
Me.p_AttachmentTempStorage.Rows.Remove(dRow)
BindAttachmentGrid(False)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End If
End Sub
By the way, the above codes are in a UserControl and the control is
place inside a plain aspx page.
Regards,
Jason
I am going nuts with this Invalid postback or callback argument thingy
with .Net 2.0
I am building a file attachment module which relays on a Datatable
stored in session (yeah i know its unhealthy, but its the only "clean"
approach which I can think of to avoid tranfering temp files onto the
server) to maintain the list of attached files before the "commit"
action is done to transfer all the files onto the server.
I have a function which allows the user to delete the attached file
before the "commit" action is performed. Essentially, the "delete" is
deleting a row from the Datatable stored in session. When I was doing
testing of this code in the early phase, all works well, but suddenly a
few days later, when I improved the codes to incorporate other
functions, whenever I click on the "delete" button (which is a template
column with an image button that triggers a "RowCommand" to handle the
deletion) i will hit the Invalid postback error. I have since removed
those new codes but I am still stuck with the problem.
Someone help me please! I am totally clueless as how to resolve this
issue. I have tried to turn off the EnableEventValidation at the page
level, but when I do so, my "RowCommand" event is not being triggered
(it works fine if i turn on the validation). You guys see the
fustration I am facing here?
Would really really be glad if someone could help me. I have attached
my codes below so you guys can help me take a look. Do let me know if
you need more information.
------------- Code Behind ---------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindAttachmentGrid(True)
Else
BindAttachmentGrid(False)
End If
vldInvalidFile.Visible = False
vldLimitExceeded.Visible = False
vldDuplicate.Visible = False
End Sub
Private Sub BindAttachmentGrid(ByVal blnBindEmpty As Boolean)
Dim dt As DataTable
If blnBindEmpty = False Then
dt = Me.p_AttachmentTempStorage <-- "this
p_AttachmentTempStorage is a private property which will determine if
an existing DT is stored in the session, if not, it will call the
CreateAttachmentDT" -->
End If
gdvAttachment.DataSource = dt
gdvAttachment.DataBind()
End Sub
Private Sub CreateAttachmentDT()
Dim dtAttach As New DataTable
dtAttach.Columns.Add("ID", GetType(Integer))
dtAttach.Columns.Add("FileName", GetType(String))
dtAttach.Columns.Add("RawFileSize", GetType(Integer))
dtAttach.Columns.Add("FileSize", GetType(String))
dtAttach.Columns.Add("UploadedBy", GetType(String))
dtAttach.Columns.Add("UploadedDateTime", GetType(String))
dtAttach.Columns.Add("FileStream",
GetType(WebControls.FileUpload))
dtAttach.Columns.Add("FileTypeFlag", GetType(String))
dtAttach.Columns.Add("FilePath", GetType(String))
dtAttach.PrimaryKey = New DataColumn() {dtAttach.Columns("ID")}
Me.p_AttachmentTempStorage = dtAttach
End Sub
Protected Sub btnAttach_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAttach.Click
If fileAttach.HasFile = True Then
Dim tmpTotalSize As Double = Me.p_TotalUploadedSize <--
"all Me.p_XXXX are simply private property which stores information in
viewstate" -->
tmpTotalSize += fileAttach.PostedFile.ContentLength
If tmpTotalSize > 10485760 Then
vldLimitExceeded.Visible = True
Exit Sub
End If
If Me.p_AllowDuplication = False Then
For Each dRowItem As DataRow In
Me.p_AttachmentTempStorage.Rows
If dRowItem("FileName").ToString.Trim =
fileAttach.FileName Then
vldDuplicate.Visible = True
Exit Sub
End If
Next
End If
Dim dRow As DataRow
Me.p_TotalUploadedSize +=
fileAttach.PostedFile.ContentLength
dRow = Me.p_AttachmentTempStorage.NewRow()
Me.p_AttachmentID += 1
dRow("ID") = Me.p_AttachmentID
dRow("FileName") = fileAttach.FileName
dRow("RawFileSize") = fileAttach.PostedFile.ContentLength
dRow("FileSize") =
ConvertFileSize(fileAttach.PostedFile.ContentLength)
dRow("UploadedBy") =
clsUser.getUserName(clsUser.getEmployeeIDByLoginID(MyBase.GetLogonUser()))
dRow("UploadedDateTime") = Format(Now,
clsUtil.ConfigValue("dateTimeFormat"))
dRow("FileStream") = fileAttach
dRow("FileTypeFlag") = "New"
dRow("FilePath") = ""
Me.p_AttachmentTempStorage.Rows.Add(dRow)
Me.p_AttachmentTempStorage.AcceptChanges()
If gdvAttachment.PageCount <> 0 Then
gdvAttachment.PageIndex = gdvAttachment.PageCount
End If
BindAttachmentGrid(False)
Else
vldInvalidFile.Visible = True
End If
End Sub
Protected Sub gdvAttachment_RowDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
gdvAttachment.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(2).Text = "Size <br>(" &
ConvertFileSize(Me.p_TotalUploadedSize) & " Total)"
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
Dim lBtnFileName As LinkButton =
CType(e.Row.FindControl("lBtnFileName"), LinkButton)
lBtnFileName.CommandArgument = e.Row.DataItem("ID")
AddHandler lBtnFileName.Click, AddressOf lBtnFileName_Click
CType(e.Row.FindControl("btnAttachmentDelete"),
ImageButton).Attributes.Add("onclick", "return confirm('Are you sure
you want to delete " & e.Row.DataItem("FileName") & "?')")
CType(e.Row.FindControl("btnAttachmentDelete"),
ImageButton).CommandArgument = e.Row.DataItem("ID")
End If
End Sub
<-- "The following code suppose to be the one to handle the delete
event, but before this code is even run, I hit the above error
already!" -->
Protected Sub gdvAttachment_RowCommand(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
gdvAttachment.RowCommand
If e.CommandName = "CustomDelete" Then
Try
Dim dRow As DataRow =
Me.p_AttachmentTempStorage.Rows.Find(e.CommandArgument)
If dRow("FileTypeFlag") = "Existing" Then
If System.IO.File.Exists(dRow("FilePath")) = True
Then
System.IO.File.Delete(dRow("FilePath"))
End If
End If
Me.p_TotalUploadedSize -= dRow("RawFileSize")
Me.p_AttachmentTempStorage.Rows.Remove(dRow)
BindAttachmentGrid(False)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End If
End Sub
By the way, the above codes are in a UserControl and the control is
place inside a plain aspx page.
Regards,
Jason