我怎样才能刷新从code一个ASP网页的背后? C# [英] How can i refresh a asp page from code behind? C#

查看:218
本文介绍了我怎样才能刷新从code一个ASP网页的背后? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这种新的,我的英语水平也较差。总之:

我试图设置刷新我的网页,如果有对我的报告的任何新的记录计时器。这显然​​是因为当我调试它,它进入功能 Timer1_Tick ,但它不刷新我的网页我的定时器正常工作。

下面是code:

  System.Timers.Timer定时器1 =新System.Timers.Timer();
Timer1.Interval = 10000;
Timer1.Elapsed + = Timer1_Tick;
Timer1.Enabled = TRUE;

 保护无效Timer1_Tick(对象发件人,EventArgs的发送){
  的Response.Redirect(ReporteIncidencia.aspx); //第一次尝试
  ScriptManager.RegisterStartupScript(页的typeof(页),somekey,RefreshPage(),真); //第2次尝试
  Page.ClientScript.RegisterStartupScript(this.GetType(),CallMyFunction,RefreshPage(),真); //第三尝试
}

 <脚本类型=文/ JavaScript的>
功能RefreshPage()
{
  window.location.reload()
}
< / SCRIPT>

编辑:


  1. 我使用.NET框架3.5

  2. 我试着用这个帖子,但它不工作。

  3. 感谢您的回答。


解决方案

我建议你使用Ajax来执行此操作。

但是,一个简单的方法来实现这一目标是使用Asp.Net定时器和更新面板组件。

在的.aspx:

 < ASP:的ScriptManager =服务器ID =ScriptManager1/>
< ASP:的UpdatePanel =服务器ID =UpdatePanel1>
<&的ContentTemplate GT;
< ASP:定时器=服务器ID =定时器间隔=10000OnTick =Timer1_Tick>
< / ASP:定时器>
< ASP:标签=服务器文本=页面没有刷新。 ID =Label1的>
< / ASP:标签>
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>

在code背后:

 保护无效Timer1_Tick(对象发件人,EventArgs的发送)
{
    Label1.Text =面板刷新在:+
    DateTime.Now.ToLongTimeString();
}

在asp:ScriptManager的组件需要使用更新面板。更多的信息在这里

I'm kind of new at this and my English is also poor. Anyway:

I'm trying to set a Timer that refreshes my page if there is any new record on my report. Apparently my timer works fine because when i debugged it it enters into the function Timer1_Tick, but it doesn't refresh my Page.

Here is the code:

System.Timers.Timer Timer1 = new System.Timers.Timer();
Timer1.Interval = 10000;
Timer1.Elapsed += Timer1_Tick;
Timer1.Enabled = true;

and

protected void Timer1_Tick(object sender, EventArgs e){
  Response.Redirect("ReporteIncidencia.aspx"); //1st attempt
  ScriptManager.RegisterStartupScript(Page, typeof(Page), "somekey", "RefreshPage()", true); //2nd attempt
  Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "RefreshPage()", true); //3rd attempt
}

also

<script type="text/javascript">
function RefreshPage()
{
  window.location.reload()
}
</script>

EDIT:

  1. I'm using Framework .NET 3.5
  2. I tried with this post but it doesn't work.
  3. Thanks for answer.

解决方案

I recommend you to use Ajax to perform this operation.

But a simple way to achieve that is using Asp.Net Timer and Update Panel components.

In .aspx:

<asp:ScriptManager runat="server" id="ScriptManager1"/>
<asp:UpdatePanel runat="server" id="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" id="Timer1" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label runat="server" Text="Page not refreshed yet." id="Label1">
</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

In code behind:

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = "Panel refreshed at: " +
    DateTime.Now.ToLongTimeString();
}

The asp:ScriptManager component is necessary to use Update Panels. More info here.

这篇关于我怎样才能刷新从code一个ASP网页的背后? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆