c#如何实时刷新数据?小编教你用微软的AJAX轻易实现的小妙招:
工具/原料
C#
AJAX
实现的基本思路:
1、拖入ScriptManager;
2、拖入UpdatePanel
3、再加入一个定时器 Timer,设置Intervql为5秒(5000)
4、加一个数据控件,根据需要GridView或DataList等等,连接数据库
5、 设置UpdatePanel为有条件异步更新
6、在Timer的Tick事件中打入代码 protected void Timer1_Tick(object sender, EventArgs e) { UpdatePanel1.Update(); GridView1.DataBind();//如果显示数据是GridView的话 }
具体的实现:
1、<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <%= DateTime.Now.ToString() %> <!--GridView控件在后台进行绑定---> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <!--定时器每5秒钟刷新一次UpdatePanel中的数据--> <asp:Timer ID="Timer1" runat="server" Interval="5000"> </asp:Timer> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"></asp:AsyncPostBackTrigger> </Triggers> </asp:UpdatePanel>