How can I have an ascx postback and refresh itself automatically every X seconds?

假如想象 提交于 2019-12-08 04:03:40

问题


I have an ascx control bound to a datasource with frequently changing data. Is there a quick way to have an ascx control postback, rebind and refresh itself every X seconds. The ascx control is in an update panel.


回答1:


Use a timer control from the AJAX toolkit since you are already using it:

<asp:Timer ID="tmrPolling" runat="server" Interval="10000" ontick="tmrPolling_Tick"></asp:Timer>

Add a trigger to your update panel like:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="tmrPolling" EventName="Tick" />
</Triggers>

Then just implement the tmrPolling_Tick handler:

protected void tmrPolling_Tick(object sender, EventArgs e)
{
    // Change your update panel controls and data here.
}

Do not add the timer within your update panel content area.




回答2:


In a client script, you can create a timer and call __doPostBack() to force the update panel to refresh. Please see this article for details.




回答3:


Repetition of code maintained for clarity/laziness.

function pageLoad(sender, args) 
{
   setTimeout(refreshPanel, 5000); // 5 seconds  
}

function requestEnd(sender, args)
{
   // Check for AJAX request errors if you'd like
   setTimeout(refreshPanel, 5000); // 5 seconds
}

function refreshPanel()
{
   __doPostBack('UpdatePanelIDHere', '');"
}

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(requestEnd);


来源:https://stackoverflow.com/questions/1732553/how-can-i-have-an-ascx-postback-and-refresh-itself-automatically-every-x-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!