Invalid postback or callback argument when navigating away from actively loading GridView

There are several posts on here with similar titles, but none that I have found actually exhibit the same behavior I’m seeing. I’m using buttons with MultiView as my navigation to give the appearance of tabs. The page loads, no problem. I can switch tabs, no problem. The issue I’m having occurs only when I press a different navigation button while a gridview is actively loading. If I wait for the gridview to fully load, I get no errors.

The full error I’m receiving is: Invalid postback or callback argument. Event validation is enabled using 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.

If I add the following, it does resolve my issue. However, I’m trying to avoid this if at all possible.

<%@ Page ... EnableEventValidation = "false" />

default.aspx
<form id="form1" runat="server">
<table width="100%" align="center">
    <tr style="background-color:#E9E9E9;">
        <td>
            <asp:Button Text="Tab1" BorderStyle="None" ID="Tab1Button" CssClass="Initial" runat="server"
                OnClick="Tab1Button_Click" />
            <asp:Button Text="Tab2" BorderStyle="None" ID="ConflictButton" CssClass="Initial" runat="server"
                OnClick="ConflictButton_Click" />
            <asp:Button Text="Tab3" BorderStyle="None" ID="Tab3Button" CssClass="Initial" runat="server"
                OnClick="Tab3Button_Click" />
            <asp:Button ID="AffiliateAddButton" runat="server" Text="Add" />
            <asp:MultiView ID="MainView" runat="server">
                <asp:View ID="View1" runat="server">
                    <table class="TabContent"><tr><td>
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataSourceID="SqlDataSource1">
                        </asp:GridView>
                    </td></tr></table>
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <table class="TabContent">
                        <tr>
                            <td>
                                View 2
                            </td>
                        </tr>
                    </table>
                </asp:View>
                <asp:View ID="View3" runat="server">
                    <table class="TabContent">
                        <tr>
                            <td>
                                View 3
                            </td>
                        </tr>
                    </table>
                </asp:View>
            </asp:MultiView>
        </td>
    </tr>
</table>
</form>

default.cs
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Tab1Button.CssClass = "Clicked";
            MainView.ActiveViewIndex = 0;

            LoadGrid();
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        // Register controls for event validation
        foreach (Control c in this.Controls)
        {
            this.Page.ClientScript.RegisterForEventValidation(
                    c.UniqueID.ToString()
            );
        }
        base.Render(writer);
    }

    private void LoadGrid()
    {
        SqlDataSource1.CancelSelectOnNullParameter = false;
        GridView1.DataSourceID = null;
        GridView1.DataSourceID = "SqlDataSource1";
        GridView1.DataBind();
    }

    private void ButtonsControl(string tab)
    {
        if(tab == "Tab1")
        {
            AffiliateAddButton.Visible = true;
            Tab1Button.CssClass = "Clicked";
            ConflictButton.CssClass = "Initial";
            Tab3Button.CssClass = "Initial";

            LoadGrid();
        }
        if (tab == "Tab2")
        {
            AffiliateAddButton.Visible = false;
            Tab1Button.CssClass = "Initial";
            ConflictButton.CssClass = "Clicked";
            Tab3Button.CssClass = "Initial";

            GridView1.DataSourceID = null;
            GridView1.DataBind();
        }
        if (tab == "Tab3")
        {
            AffiliateAddButton.Visible = false;
            Tab1Button.CssClass = "Initial";
            ConflictButton.CssClass = "Initial";
            Tab3Button.CssClass = "Clicked";

            GridView1.DataSourceID = null;
            GridView1.DataBind();
        }
    }

    protected void Tab1Button_Click(object sender, EventArgs e)
    {
        ButtonsControl("Tab1");
        MainView.ActiveViewIndex = 0;
    }

    protected void ConflictButton_Click(object sender, EventArgs e)
    {
        ButtonsControl("Tab2");
        MainView.ActiveViewIndex = 1;
    }

    protected void Tab3Button_Click(object sender, EventArgs e)
    {
        ButtonsControl("Tab3");
        MainView.ActiveViewIndex = 2;
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

What I ended up doing was two things:

  1. Added paging. Ideally I didn’t want this in this specific scenario but limiting my page to 500 lines made it load fast enough to almost eliminate this.
  2. Switched from multiview to frameset. Again, not an ideal option, but works in my given scenario.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x