Search code examples
asp.netupdatepanelpage-refresh

using alternative of updatepanel


I have gridview in update panel and i am doing post back after two seconds to update panel like this

<script language="javascript" type="text/javascript">
        window.setInterval(function () {
   __doPostBack('<%= UpdatePanel1.ClientID %>', '');
        }, 2000);
</script>

and on server side i used this code to load grid on each postback

if (IsPostBack)
        {
            GridView2.DataBind();
            SqlDataSource1.DataBind();
        }

It Load the Gridview after tow seconds accordingly.But Problem is that it didn't work on a bit slow internet connection or also making load on web server i only want to ask that is there any alternative on client end or with javascript so it should do all on client level not on server level.Plz give any better idea


Solution

  • To be blunt, this is about the worst possible use of an UpdatePanel. You are rendering the entire page (behind the scenes) just to update that panel every 2 seconds.

    There are a few alternatives:

    1. Page Methods
    2. HTTP-enabled WCF endpoint which serves the content in response to an AJAX request.
    3. MVC controller integrated into your Web Forms application, which also responds to an AJAX request.

    All of these approaches will not require the page to be rendered, just the content that you actually need. Thus, they are immediately more efficient.

    Options #2 and #3 will require a little manual JavaScript to make the AJAX call. jQuery works nicely for this.

    However, you still are dealing with the fact that you must poll the server every few seconds. To avoid this, you may want to read about long polling to achieve push notifications instead.

    See also: ASP.Net SignalR