views:

3773

answers:

2

I have a page that is hitting a webservice every 5 seconds to update the information on the page. I'm using the DynamicPopulateExtender from the Ajax Control Toolkit to just populate a panel with some text.

What I was wanting to do, is if a certain condition is met, to refresh the page completely.

Am I going to be able to do this in the current method that I have? here's my current stuff:


ASP.NET

<cc1:DynamicPopulateExtender ID="DynamicPopulateExtender1" runat="server"
ClearContentsDuringUpdate="true" TargetControlID="panelQueue" BehaviorID="dp1"
ServiceMethod="GetQueueTable" UpdatingCssClass="dynamicPopulate_Updating" />

Javascript

Sys.Application.add_load(function(){updateQueue();});

    function updateQueue()
    {
        var queueShown = document.getElementById('<%= hiddenFieldQueueShown.ClientID %>').value;

        if(queueShown == 1)
        {
            var behavior = $find('dp1');
            if (behavior)
            {
                behavior.populate();                    
                setTimeout('updateQueue()', 5000);
            }
        }
    }

SERVER (C#)

[System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string GetQueueTable()
    {
        System.Text.StringBuilder builder = new System.Text.StringBuilder();

        try
        {
             // do stuff
        }
        catch (Exception ex)
        {
             // do stuff
        }

        return builder.ToString();
    }
+4  A: 
  • You can't do anything from your ASMX.
  • You can refresh the page from JavaScript by using a conventional page reload or by doing a postback that would perform server-side changes and then update via your UpdatePanel or, more simply, a Response.Redirect.
Jason Kealey
+1  A: 

You can force a Postback from Javascript, see this Default.aspx page for a example:


Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript" language="javascript">
        function forcePostback()
        {
            <%=getPostBackJavscriptCode()%>;
        }
    </script>

</head>

<body onload="javascript:forcePostback()">
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Postbacking right now..."></asp:Label>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

namespace ForcingApostback
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) Label1.Text = "Done postbacking!!!";
        }

        protected string getPostBackJavscriptCode()
        {
            return ClientScript.GetPostBackEventReference(this, null);

        }
    }
}

On the client-side, under any condition, you could then call the forcePostback() Javscript function to force the Postback.

Hope this helps.

Alfred B. Thordarson