0

我需要从 HttpModule 永久或不时更新 ASP.NET 页面。

下面是我们要更新的页面的 IUpdatablePage 接口代码:

    interface IUpdatablePage
    {
       void Update( string value );
    }

这是HttpModule的代码,我想,可以是:

    void IHttpModule.Init( HttpApplication application )
    {
       application.PreRequestHandlerExecute += new EventHandler( application_PreRequestHandlerExecute );
    }
    void application_PreRequestHandlerExecute( object sender, EventArgs e )
    {
       this._Page = ( Page )HttpContext.Current.Handler;
    }

    void HttpModuleProcessing()
    {
       //... doing smth

       IUpdatablePage page = this._Page as IUpdatablePage;
       page.Update( currentVaue );

       //... continue doing smth
    }

在这里,我们:

  1. 将当前请求页面保存在 _Page 中,
  2. 在 HttpModule 中处理时访问 IUpdatablePage 接口
  3. 调用传递一些 currentValue 的更新函数。

现在页面获取更新函数中的值。

    public partial class MyPage: System.Web.Page, IUpdatablePage
    {
       void IUpdatablePage.Update( string value )
       {
          // Here we need to update the page with new value
          Label1.Text = value;
       }
    }

问题是如何将此值传输到 webform 控件以便它们立即在浏览器中显示它?

我想刷新页面的任何方式:使用 UpdatePanel、Timer、iframe 块、javascript 等。

注意,来自页面的请求在刷新时正在 HttpModule 中处理。请帮助提供代码示例(我是网络初学者)。

4

1 回答 1

0

在 Page 和 HttpModule 之间传输数据的方法是使用应用程序命名的静态对象,这些对象由会话 id 标识。该页面通过由计时器触发的 UpdatePanel 进行更新。

HttpModule 的代码(简化):

public class UploadProcessModule : IHttpModule
{
   public void Init( HttpApplication context )
   {
      context.BeginRequest += context_BeginRequest;
   }
   void context_BeginRequest( object sender, EventArgs e )
   {
      HttpContext context = ( ( HttpApplication )sender ).Context;
      if ( context.Request.Cookies["ASP.NET_SessionId"] != null )
      {
          string sessionId = context.Request.Cookies["ASP.NET_SessionId"].Value;
          context.Application["Data_" + sessionId] = new MyClass();
      }
   }
}
于 2013-07-09T16:52:49.213 回答