Link to home
Start Free TrialLog in
Avatar of rckrch
rckrchFlag for United States of America

asked on

Error with postback when using gridview in asp.net vb.net

I have a grid view on a master page that is in an update panel which is in a panel and this is used with a popup extender.  The gridview works ok, but the 'Done' button returns an error when trying to close the popup after making changes to the Grid view with the row commands.  This has to do with the post back, but I don't know how to fix.  The 'Done' button works fine if I don't make any changes to the grid view.  I have left the page EventCausesValidation=true by default.

This is the error:  
JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: 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.

Please help.  Thanks

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If IsPostBack = True Then
            
        Else
            

            CoreQuestDataSource.DataBind()
            CoreQuestGrdVw.DataBind()

           
        End If


    End Sub

Open in new window


Protected Sub CoreQuestGrdVw_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles CoreQuestGrdVw.RowCommand
        If e.CommandName = "Update" Then
            Dim GrdVwRw As GridViewRow = DirectCast(DirectCast(e.CommandSource, WebControls.LinkButton).NamingContainer, GridViewRow)
            Dim RecID As Integer = DirectCast(GrdVwRw.FindControl("RecordID"), WebControls.HiddenField).Value.ToString
            Dim WCQ As String = DirectCast(GrdVwRw.FindControl("txtWCQ"), WebControls.TextBox).Text
            CoreQuestDataSource.UpdateParameters("RecordID").DefaultValue = RecID
            CoreQuestDataSource.UpdateParameters("WorkCell_Specific").DefaultValue = WCQ
            CoreQuestDataSource.DataBind()
            CoreQuestGrdVw.DataBind()

        ElseIf e.CommandName = "Insert" Then
            Dim NewQ As String = DirectCast(CoreQuestGrdVw.FooterRow.FindControl("ftrtxtWCQ"), WebControls.TextBox).Text
            CoreQuestDataSource.InsertParameters("WorkCell_Specific").DefaultValue = NewQ
            CoreQuestDataSource.DataBind()
            CoreQuestGrdVw.DataBind()


        End If
    End Sub
    Protected Sub CoreQuestGrdVw_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles CoreQuestGrdVw.RowEditing
        CoreQuestGrdVw.EditIndex = e.NewEditIndex


    End Sub
    Protected Sub CoreQuestGrdVw_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles CoreQuestGrdVw.RowUpdating
        CoreQuestGrdVw.EditIndex = -1



    End Sub

Open in new window


<asp:Panel ID="CoreQuest" runat="server" Width="40%" Visible="True" ViewStateMode="Enabled"
                    BackColor="#E9E9E9" BorderColor="#999999" BorderStyle="Inset" BorderWidth="1">
    <asp:UpdatePanel ID="UpDtCoreQuest" runat="server" UpdateMode="Conditional" ViewStateMode="Enabled">
    <ContentTemplate>
        <br />
        <asp:Label ID="Label6" runat="server" Text="Core ...Library" Font-Names="Arial" 
        Font-Size="14" ForeColor="#003366" ></asp:Label>
        <br /><br />
        <asp:GridView ID="CoreQuestGrdVw" runat="server" AlternatingRowStyle-BackColor="#CCCCCC"
                BackColor="White" BorderColor="#003366" BorderStyle="Inset" BorderWidth="2px"
                EnablePersistedSelection="True" Font-Bold="False" Font-Names="Arial" Font-Size="10pt"
                ForeColor="#003366" GridLines="Vertical" HeaderStyle-BackColor="#336699" HeaderStyle-BorderColor="#003366"
                HeaderStyle-BorderStyle="Inset" HeaderStyle-BorderWidth="1" HeaderStyle-ForeColor="#FFFFCC"
                HeaderStyle-Height="20" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle"
                HeaderStyle-Wrap="False" HorizontalAlign="Center" RowStyle-BackColor="White"
                RowStyle-ForeColor="#003366" RowStyle-HorizontalAlign="Center" RowStyle-VerticalAlign="Middle"
                SelectedRowStyle-ForeColor="#003366" ViewStateMode="Enabled" Width="80%" DataKeyNames="RecordID"
                AutoGenerateColumns="False" Height="75%" ShowFooter="True" DataSourceID="CoreQuestDataSource"
                OnRowCommand="CoreQuestGrdVw_RowCommand" ShowHeaderWhenEmpty="True">
                <EditRowStyle BackColor="#FF5050" ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" 
                Width="95%" />
                <Columns>
                <asp:CommandField ShowEditButton="true"/>
                    <asp:TemplateField Visible="False">
                        <ItemTemplate>
                            <asp:HiddenField runat="server" ID="RecordID" Value='<%# Eval("RecordID") %>' Visible="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField Visible="True">
                        <FooterTemplate>
                            <asp:Button ID="FtrInsrtBtn" runat="server" Text="Insert" CommandName="Insert" BackColor="Transparent"
                                BorderStyle="None" Font-Names="Arial" Font-Size="10" ForeColor="#003366" Font-Underline="True" />
                            <asp:Button ID="FtrCncltBtn" runat="server" Text="Cancel" CommandName="Cancel" BackColor="Transparent"
                                BorderStyle="None" Font-Names="Arial" Font-Size="10" ForeColor="#003366" Font-Underline="True" />
                        </FooterTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblWCQ" Text='<%# Eval("WorkCell_Specific") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtWCQ" runat="server" Width="95%" Text='<%# Eval("WorkCell_Specific") %>'>
                            </asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="ftrtxtWCQ" runat="server" Width="95%"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                
                </Columns>
        </asp:GridView>
        <asp:Button ID="CoreQDone" runat="server" Text="Done" OnClick="CoreQDone_Click" CausesValidation="True" />
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="CoreQuestGrdVw" EventName="RowCommand" />
    <asp:AsyncPostBackTrigger ControlID="CoreQDone" EventName="Click" />
    </Triggers>
    
    </asp:UpdatePanel>
    <asp:ModalPopupExtender ID="CoreQuestPopup" runat="server"   
    PopupControlID="CoreQuest" CancelControlID="CoreQDone" Enabled="True" TargetControlID="MstrQLst" 
    ViewStateMode="Enabled">
    </asp:ModalPopupExtender>
    </asp:Panel>
       <asp:SqlDataSource ID="CoreQuestDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ManualDataConnection %>" 
            ProviderName="<%$ ConnectionStrings:ManualDataConnection.ProviderName %>" 
            SelectCommand="SELECT * FROM [L...eet]"
            InsertCommand="INSERT INTO L...eet (WorkCell_Specific) VALUES (@WorkCell_Specific)"
            UpdateCommand="UPDATE L...t SET WorkCell_Specific=? WHERE RecordID=?">
            <InsertParameters>
            <asp:Parameter Name="WorkCell_Specific" DefaultValue="WorkCell_Specific" Type="String"/>
            </InsertParameters>
            <UpdateParameters>
            <asp:Parameter Name="RecordID" DefaultValue="RecordID" Type="Int32" />
            <asp:Parameter Name="WorkCell_Specific" DefaultValue="WorkCell_Specific" Type="String" />
            </UpdateParameters>
            
    </asp:SqlDataSource>

Open in new window

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi rckrch,

You already have your asnwers
1.  On your page, set enableEventValidation to false by using <%@ Page EnableEventValidation = "false" %>. Do not turn it off sitewide, only where it causes these errors(especially AJAX enabled pages). You can also enable partial rendering in your ScriptManager - in some cases that will help to fix this error without disabling the validation.
2. Use ClientScriptManager.RegisterForEventValidation Method (String) https://msdn.microsoft.com/en-us/library/ms223397(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 and provide your own mechanism to handle event validation.

Let me know if your run into any issues.

Regards,
Chinmay.
Avatar of rckrch

ASKER

Thanks Chinmay.  I appreciate you comment.  However, I have tried the EnableEventValidtion="False" and the page level and it does not work.  Also, I have tried to use the ClientScripManager.RegisterForEvaneValidation - but don't know how to use it.

I always use partial rendering.
Avatar of rckrch

ASKER

This issue is on a master page.
When you set EnableEventValidtion to false, this same error occurs? Can you post the <% page> tag here please?
Avatar of rckrch

ASKER

<%@ Master Language="VB" CodeFile="LPAMaster.master.vb" Inherits="LPAMaster" AutoEventWireup="false" %>
Please try
<%@ Master Language="VB" CodeFile="LPAMaster.master.vb" Inherits="LPAMaster" AutoEventWireup="false" EnableEventValidation = "false"  %

Open in new window

Avatar of rckrch

ASKER

When I change it to this:
<%@ Master Language="VB" CodeFile="LPAMaster.master.vb" Inherits="LPAMaster" AutoEventWireup="false" EnableEventValidation="false"%>

I get this:
Error parsing 'enableeventvalidation': Type 'System.Web.UI.MasterPage' does not have a public property named enableeventvalidation.
Oh. My bad. The Page directive w are trying to set does not exists on MasterPage. It has to be set on ContentPage.

On which page you are getting the error?

the page that is giving you error, please post its <%Page %> tag.
OR
use the following
<%@ Page Language="VB" CodeFile="YOUR CODE FILE" Inherits="YOUR CLASS NAME" AutoEventWireup="false" EnableEventValidation="false"%>

Open in new window

Avatar of rckrch

ASKER

This gridview and popup is on a master page.
Strange. Any specific reason, to put gridview on a master page?

As a last resort, please modify your web.config

<system.web>
    <pages 
         .... ...
         enableEventValidation="false">
    </pages>
  </system.web>

Open in new window


add enableEventValidation="false".

Regards,
Chinmay.
Avatar of rckrch

ASKER

The GridView is in a popup.  This popup is triggered from an "Open" button in a menu bar (in case just a table with buttons - some navigation and some like this one is popup.  I am doing this to allow the user to update some 'Library data' if needed in order to complete the events on the content page.

When I change the system config file, isn't that changing the site for the eventValidation?
Yes. It does. How many items you are adding dynamically in the grid view?
Avatar of rckrch

ASKER

It depends on the users.  The girdview itself works fine with updating the database.  I have tested it with data updates using the grid view multiple times with no issue.  The problem is with the 'Done' button on the panel.  That is when I get the error.
I just noticed

<asp:Button ID="CoreQDone" runat="server" Text="Done" OnClick="CoreQDone_Click" CausesValidation="True" />

Open in new window

Please set it to
<asp:Button ID="CoreQDone" runat="server" Text="Done" OnClick="CoreQDone_Click" CausesValidation="False" />

Open in new window

Avatar of rckrch

ASKER

Thanks I tried that too.  It gives me the same error.  I changed it back to 'true' because it did not have an effect.
I think now there is only one option left for us. As your grid is dynamic, I am not sure from where else this could be originating.

So, let's try registering all of them for validation.

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    For Each control As Control In Me.Controls
        Me.Page.ClientScript.RegisterForEventValidation(control.UniqueID.ToString())
    Next control
    MyBase.Render(writer)
End Sub

Open in new window

Avatar of rckrch

ASKER

I get an error saying: 'UniqueID' is not a member of System.Windows.Forms.Control
Avatar of rckrch

ASKER

I tried this before <head> and before <html> and I get the same error as before.

<script runat="server">
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        For Each control As Control In Me.Controls
            Page.ClientScript.RegisterForEventValidation(control.UniqueID, Me.ToString())
        Next control
        MyBase.Render(writer)
    End Sub
</script>
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rckrch

ASKER

Well, Chinmay,

Thanks for all your help.

I solved this problem.  I removed all of the RegisterForEnableEventvalidation code we did and set everything back to default.  All I did was move the button that was causing the problem from inside the update panel to outside the update panel, inside the panel.

Now it does work correctly.