Search code examples
c#asp.netdatagridviewstate

ASP.NET Invalid Viewstate on Datagrid Sort if user clicks twice


I'm having an issue on an older bit of code with an asp:Datagrid. This Datagrid has sorting columns enabled and if the user clicks too quickly, before the page finishes reloading from the sort, an invalid viewstate error is thrown. I tried disabling viewstate on the control and then rebinding it in every page load but to no avail. Anyone know how I can fix this?


Solution

  • So I ended up having to disable the links client side once a link was clicked. I used jquery to grab all my link tags in the DataGrid (since the only ones I had in there were in the header) and gave them all a click event that, once clicked, gave all link tags another click event that prevented the click.

    To whit:

    $(document).ready(function() {
        $("#<%=myDataGrid.ClientID%> a").on("click", function() {
            $("#<%=myDataGrid.ClientID a").on("click", function(e) {
                e.preventDefault();
            });
        });
    });
    

    In this way only the first click fires and all others are prevented until after the page refreshes from the post back.