现在的位置: 首页 > 综合 > 正文

asp.net自动刷新获取数据

2012年01月08日 ⁄ 综合 ⁄ 共 4380字 ⁄ 字号 评论关闭

  2011就这样悄悄地走来,总结过去的一年,从中发现了自己的进步和不足,我将在2011里保持和改进,进一步提升自己。

  今天将是我兔年的第一篇文章,这篇文章是受人启发而来。之前有同学问我关于Dundas Gauge控件绘图的问题,他提到一个关于每隔1秒就需要从数据库提取数据的问题。这个问题也是第一次碰到,正好这几天也不忙,就抽时间研究一番。发现有两种方法可以去解决:

  MagicAjax 下载地址:http://www.codepub.com/d/downpage.php?n=1&id=15015::1238911050

  PopupWin 下载地址:http://www.51aspx.com/CV/PopupWin/

 

第一种方法:可以使用 MagicAjax 控件

添加引用MagicAjax.dll

创建Default.aspx页面:

代码

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2  <%@ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>
3  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5  <html xmlns="http://www.w3.org/1999/xhtml" >
6  <head runat="server">
7 <title>MagicAjax入门程序</title>
8  </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <ajax:ajaxpanel ID="AjaxSourcePanel" runat="server" Height="128px" Width="144px">
13 <asp:Button ID="Button1" runat="server" Text="点击查看" OnClick="Button1_Click" />
14 <br />
15 当前时间:
16 <br />
17 <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="Red"></asp:Label>
18 </ajax:ajaxpanel>
19 </div>
20 </form>
21 </body>
22 </html>

 后台代码:

代码

1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using MagicAjax;
11 using EeekSoft.Web;
12
13 public partial class _Default : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!Page.IsPostBack)
18 {
19 MagicAjax.AjaxCallHelper.SetAjaxCallTimerInterval(1000);
20 }
21 Label1.Text = DateTime.Now.ToString();
22 }
23 protected void Button1_Click(object sender, EventArgs e)
24 {
25 Label1.Text = DateTime.Now.ToString();
26 MagicAjax.AjaxCallHelper.Write("alert('" + "ZHF一个小测试!" + "');");
27 }
28 }
29

 

第二种方法:可以使用 UpdatePanel 和 Timer 控件

创建popupWin.aspx页面:

代码

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="popupWin.aspx.cs" Inherits="popupWin" %>
2 <%@ Register Assembly="EeekSoft.Web.PopupWin" Namespace="EeekSoft.Web" TagPrefix="cc1" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:ScriptManager ID="sm" runat="server" />
13 <asp:Button ID="Button2" runat="server" Text="显示消息" onclick="Button2_Click" />
14 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
15 <ContentTemplate>
16 <cc1:PopupWin ID="popupw" runat="server" ColorStyle="red" Title="欢迎您登陆管理页面" DragDrop="False"
17 Height="109px" Width="158px" AutoShow="true" ActionType="RaiseEvents"
18 DockMode="BottomRight" style="top: 31px; left: 49px" Visible="false"
19 HideAfter="3000"></cc1:PopupWin>
20 </ContentTemplate>
21 </asp:UpdatePanel>
22 <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
23 <ContentTemplate>
24 <font color="red">当前消息数:</font>
25 <asp:Label ID="lbshow" runat="server" ForeColor="red"></asp:Label>
26 <hr />
27 <asp:Label ID="Label2" runat="server" Text="Label" Font-Size="Medium"
28 ForeColor="Red"></asp:Label>
29 <asp:Timer ID="Timer1" runat="server" Enabled="true" Interval="1000"
30 ontick="Timer1_Tick">
31 </asp:Timer>
32 </ContentTemplate>
33 </asp:UpdatePanel>
34 </div>
35 </form>
36 </body>
37 </html>

 后台代码:

代码

1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Web;
5  using System.Web.UI;
6  using System.Web.UI.WebControls;
7
8  public partial class popupWin : System.Web.UI.Page
9 {
10 public static int count = 0;
11 protected void Page_Load(object sender, EventArgs e)
12 {
13 if (!Page.IsPostBack)
14 {
15 lbshow.Text = count.ToString();
16 Label2.Text = DateTime.Now.ToString();
17 }
18 }
19 protected void showPop()
20 {
21 string msg = "";
22 msg += "<a href=\"index.aspx\" target=\"mainFrame\" >欢迎来这里</a><br />";
23
24 popupw.Message = msg;
25 popupw.Title = "有新的消息";
26 popupw.ColorStyle = EeekSoft.Web.PopupColorStyle.Red;
27 popupw.DockMode = EeekSoft.Web.PopupDocking.BottomRight;
28 popupw.WindowScroll = true;
29 popupw.HideAfter = 2000; ///设置自动隐藏时间 -1:无限
30   popupw.ShowAfter = 1000;//打开页面窗体时间
31   popupw.AutoShow = true;
32 popupw.Visible = true;
33 }
34 protected void Timer1_Tick(object sender, EventArgs e)
35 {
36 count = count + 1;
37 lbshow.Text = count.ToString();
38 Label2.Text = DateTime.Now.ToString();
39 }
40 protected void Button2_Click(object sender, EventArgs e)
41 {
42 showPop();
43 }
44 }
45  

 

  代码中获取时间的位置可以换成从数据库查询记录集的方法,只要自动刷新页面的问题搞定了,其它问题也就迎刃而解了

  存在问题:本来还想借助popupWin实现隔几秒就弹出消息提示框,试了好几种方法就是没反应。

  不知道高手有没有遇见类似的问题,希望能指点一下。

 

抱歉!评论已关闭.