0

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;
    }
aantiix
  • 516
  • 2
  • 9
  • 24
  • One approach you could take is setting a flag when the nav fires, then setting it to something else when its done. on the nav fire you first check the value before processing or perhaps show a div loader or update progress until its done, then fire the nav. – JobesK Sep 04 '20 at 21:44
  • Does this link help you? https://stackoverflow.com/a/229041/6029001 – Rohan Rao Sep 05 '20 at 03:34
  • @JobesK I’m not sure how that would help as I don’t see why this behavior is happening so I don’t know what logic I’d need to do differently if the page was changed during grid view loading. Unless you’re suggesting I prevent users from doing this until after the page loads, but that’s not a good solution in this case. I need to let users change pages immediately if desired. – aantiix Sep 05 '20 at 13:27
  • @noobprogrammer no that page is not the same as the issue I’m experiencing but if you look at my initial message you’ll see I tried those things anywhere (where they applied) but the behavior I’m seeing, persists. – aantiix Sep 05 '20 at 13:31

1 Answers1

0

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.
aantiix
  • 516
  • 2
  • 9
  • 24