0

I'm getting the old "Validation of viewstate MAC failed" error when I click the submit button on my form. (See http://blogs.msdn.com/b/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx) The thing is I'm getting the error even when the page is completely loaded (it can be sitting there for five minutes before I hit submit but I still get the error). This isn't even a particularly lengthy page. It uses a DetailsView (which I understand is one of the preconditions for this error), but it's the only databound control on the page. Also, whenever users get this error (which is 100% of the time when they click Submit), they lose their login authentication and have to log back in. This page/site has been in use for about six months but the error suddenly started without any changes being made to the code.

I'm using ASP.net 4 on shared hosting.

Any ideas? Thanks

Edited by georgegra: Clarification

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by georgegra
0

Maybe it would help if I post the relevant code/markup:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" 
                CssClass="ModuleContent" DataSourceID="DocumentsDataSource" 
                DefaultMode="Insert" Visible="False">
                <Fields>
                    <asp:TemplateField HeaderText="Text" SortExpression="Text">
                        
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextTextBox" runat="server" Text='<%# Bind("Text") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Enter text to display for the document" ControlToValidate="TextTextBox"
                                runat="server" CssClass="ErrorText" EnableClientScript="False" ValidationGroup="DetailsViewValidationGroup" />
                        </InsertItemTemplate>
                        
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Category" SortExpression="Category">
                        
                        <InsertItemTemplate>
                            <asp:DropDownList ID="CategoryDropDownList" runat="server" 
                                SelectedValue='<%# Bind("Category") %>'>
                                <asp:ListItem>Brochures</asp:ListItem>
                                <asp:ListItem>Calendar</asp:ListItem>
                                <asp:ListItem>General</asp:ListItem>
                                <asp:ListItem Value="TheFollowing">The Following</asp:ListItem>
                                <asp:ListItem Value="MissionTrip">Mission Trip</asp:ListItem>
                            </asp:DropDownList>
                        </InsertItemTemplate>
                        
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Upload File">
                    <InsertItemTemplate>
                    <asp:FileUpload ID="FileUpload1" runat="server"
                 CssClass="ModuleContentWithoutMargins" /><asp:Literal
                ID="Literal1" runat="server" Text="<br />"></asp:Literal>
            <asp:Button ID="UploadFileButton" runat="server" Text="Upload File" 
                            CssClass="ModuleContentWithoutMargins" onclick="UploadFileButton_Click" CausesValidation="false" ViewStateMode="Inherit" />
            <br />

            <asp:Label ID="FileExtensionErrorLabel" runat="server" style="Color: Red"
                Text="The file must be in .doc, .docx, .pdf, .rtf, .txt, or .xps format." 
                Visible="False"></asp:Label>
                <asp:Label ID="NoFileSelectedErrorLabel" runat="server" Text="Choose a file to upload." Visible="false" CssClass="ErrorText"></asp:Label>
                <asp:Label ID="FileNameErrorLabel" runat="server" Text="A file with this name has already been uploaded. Re-name the file you are uploading or delete the file that has already been uploaded." Visible="false" CssClass="ErrorText"></asp:Label>
                <asp:Label ID="OtherErrorLabel" runat="server" Text="The file could not be uploaded. Please contact the webmaster." Visible="false" CssClass="ErrorText"></asp:Label>       
                        <asp:TextBox ID="URLTextBox" runat="server" Text='<%# Bind("URL") %>' Visible="false"></asp:TextBox>       
                    </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <InsertItemTemplate>
                            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" 
                                CommandName="Insert" Text="Add Document" Enabled="False" CssClass="ModuleContentWithoutMargins" ValidationGroup="DetailsViewValidationGroup" />

                            <asp:Button ID="CancelButton"  runat="server" CausesValidation="False" 
                                CommandName="Cancel" Text="Cancel" CssClass="ModuleContentWithoutMargins" />
                            
                        </InsertItemTemplate>
                       </asp:TemplateField>
                </Fields>
            </asp:DetailsView>

And here's my codebehind:

Partial Class documentupload
    Inherits System.Web.UI.Page

    Protected Sub UploadFileButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'Make the controls within DetailsView1 accesible in this sub
        Dim FileUpload1 As FileUpload = DetailsView1.FindControl("FileUpload1")
        Dim FileExtensionErrorLabel As Label = DetailsView1.FindControl("FileExtensionErrorLabel")
        Dim FileNameErrorLabel As Label = DetailsView1.FindControl("FileNameErrorLabel")
        Dim OtherErrorLabel As Label = DetailsView1.FindControl("OtherErrorLabel")
        Dim NoFileSelectedErrorLabel As Label = DetailsView1.FindControl("NoFileSelectedErrorLabel")
        Dim UploadFileButton As Button = DetailsView1.FindControl("UploadFileButton")
        Dim Literal1 As Literal = DetailsView1.FindControl("Literal1")
        Dim InsertButton As Button = DetailsView1.FindControl("InsertButton")
        Dim URLTextBox As TextBox = DetailsView1.FindControl("URLTextBox")

        'Reset all error labels
        FileExtensionErrorLabel.Visible = False
        FileNameErrorLabel.Visible = False
        OtherErrorLabel.Visible = False
        NoFileSelectedErrorLabel.Visible = False

        Dim path As String = Server.MapPath("~/Documents/")
        Dim FileExtensionOkay As Boolean = False
        Dim FileNameOkay As Boolean = False

        If FileUpload1.HasFile Then

            Dim FileExtension As String = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower()
            Dim AllowedExtensions As String() = {".doc", ".docx", ".pdf", ".rtf", ".txt", ".xps"}
            For i As Integer = 0 To AllowedExtensions.Length - 1
                If FileExtension = AllowedExtensions(i) Then
                    FileExtensionOkay = True
                End If
            Next
            If FileExtensionOkay = False Then
                FileExtensionErrorLabel.Visible = True
            End If

            Dim PathToCheckWhetherFileExists As String = Server.MapPath("~/Documents/") & FileUpload1.FileName
            If System.IO.File.Exists(PathToCheckWhetherFileExists) = False Then
                FileNameOkay = True
            Else
                If FileExtensionOkay = True Then
                    FileNameErrorLabel.Visible = True
                End If
            End If

            If FileExtensionOkay = True And FileNameOkay = True Then
                Try
                    FileUpload1.PostedFile.SaveAs(path & FileUpload1.FileName)
                    FileUpload1.Visible = False
                    UploadFileButton.Text = "File Uploaded"
                    UploadFileButton.Enabled = False
                    Literal1.Text = ""
                    InsertButton.Enabled = True
                    URLTextBox.Text = CStr(FileUpload1.FileName)

                Catch ex As Exception
                    OtherErrorLabel.Visible = True
                End Try
            End If
        Else
            NoFileSelectedErrorLabel.Visible = True
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If User.IsInRole("Administrator") Then
            DetailsView1.Visible = True
        Else
            Response.Redirect("default.aspx")
        End If
    End Sub

    Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
        Response.Redirect("documents.aspx")
    End Sub
End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.