0

I have ASP Web Forms Web application, which is used for searching in my database. I have page which contains gridview and also text input field for filtering results.

Input field has onkeyup event which run postback by JavaScript for refreshing the gridview from codebehind.

And also in codebehind I have method, which does saving state last filter request, and if request do not equal previously filter request, then I run new query to database.

My problem - onkeyup event on text input field may generate multiple postbacks. If it first postback, then server will execute the query to database. But following postbacks do not execute and overlap first results. And therefore my gridview stay old state.

<script type="text/javascript">
function DoUpdateGridView() {
    var timeout = null;
    clearTimeout(timeout);
    timeout = setTimeout(function () {
        Sys.WebForms.PageRequestManager.getInstance()._doPostBack('<%=GUIGridUpdatePanel.ClientID%>', '<%=GUIGridUpdatePanel.UniqueID%>');
    }, 1000);

};
</script>

How you may see I set delay for execute JavaScript, but this only Delays execution of the code but does not interrupt.

I think i may make cache first results, and return it. But I need know all solutions for this problem.

Сan there be any way to perform only the latest event from the JavaScript? But then somebody may send multiple request and my web application may fall (DDOS).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0

When two (or more) postbacks are made from the same client at the same time, they send the same ViewState data to the server as part of each request. Control state is part of ViewState. Assuming Session is being handled in the default manner, these requests are processed by the server serially.

Let's say the server alters controls while handling the first request. Then the second request is processed. But since both postbacks were generated at the same time, the second request has the same ViewState (and thus the same control state) as the first request, so the state of the controls that the server sees is the state before the first request was processed, and not the state after the first request was processed!

The simplest solution is to store the pieces of state you are using to determine whether to change the controls in the Session, which by default is stored in memory on the server, thus making the two requests distinguishable.

Daniel O
  • 336
  • 2
  • 3
  • 8
0

You can't interrup what is already happening on the server, but there is an issue with your "delay" code. You need to persist the timeout variable between calls.

Try:

<script type="text/javascript">
//New Position for timeout variable
var timeout = null;
function DoUpdateGridView() {     
    //This will now clear the existing timneout function   
    clearTimeout(timeout);
    timeout = setTimeout(function () {
       //For debug purposes only
       console.log("About to postback");
       Sys.WebForms.PageRequestManager.getInstance()._doPostBack('<%=GUIGridUpdatePanel.ClientID%>', '<%=GUIGridUpdatePanel.UniqueID%>');
    }, 1000);

};
</script>
Jon P
  • 19,442
  • 8
  • 49
  • 72