简体   繁体   English

从ASP.NET中的静态类更新控件的值

[英]Update a control's value from a static class in ASP.NET

Say that I have an ASP.NET page with a Label control and the following static class which executes a scheduled job: 假设我有一个带有Label控件的ASP.NET页和执行计划作业的以下静态类:

public static class Job
{
    // The Execute method is called by a scheduler and must therefore 
    // have this exact signature (i.e. it cannot take any paramters).
    public static string Execute()
    {
        // Do work
    }
}

When the job is done, the execute method should update the value of the Label control on the page. 作业完成后,execute方法应更新页面上Label控件的值。

I've done some research and the only way seems to be to use HttpContext.Current.CurrentHandler. 我已经做过一些研究,唯一的方法似乎是使用HttpContext.Current.CurrentHandler。 However, this is undesirable for me since it can potentially return null. 但是,这对我来说是不可取的,因为它可能会返回null。

Since the Execute method cannot take any parameters (see comment), passing the Page instance as an argument is not an option. 由于Execute方法不能接受任何参数(请参见注释),因此不能将Page实例作为参数传递。

Is there any other way to update the control from the static class? 还有其他方法可以从静态类更新控件吗?

NOTE: the Execute method must be static because I'm creating an EPiServer scheduled job, which requires a static Execute method (that doesn't take any parameters). 注意:Execute方法必须是静态的,因为我正在创建EPiServer计划作业,该作业需要静态Execute方法(不带任何参数)。

If the job is not executed synchronously (or even if it is), I think that you may want to consider the order of control. 如果作业不是同步执行(即使执行不了),我认为您可能需要考虑控制顺序。

What I suggest in a case like this is a structure similar to the following: 在这种情况下,我建议的结构类似于以下内容:

1) The web page issues the request for the job 1)网页发出工作请求

2) Somewhere, a unique reference to the job is created and stored (such as GUID or an identity column in a database table) 2)在某个地方,将创建并存储对作业的唯一引用(例如GUID或数据库表中的标识列)

3) The job is executed asynchronously by code-behind and then the unique identifier is returned to the web page. 3)通过后台代码异步执行作业,然后将唯一标识符返回到网页。

4) The web page, on startup, initiates a javascript method (using window.timeout, for example) that on a regular basis, issues an ajax query to the web server to check on the status of the job. 4)网页在启动时会启动javascript方法(例如,使用window.timeout),该方法会定期向网络服务器发出ajax查询,以检查作业的状态。

5) When the job is complete, it updates the global reference with the appropriate information. 5)作业完成后,它将使用适当的信息更新全局参考。

6) When the javascript sees that the job is complete, it updates the label. 6)当javascript看到作业已完成时,它将更新标签。

This process allows the user to carry on with other work if necessary and not have to worry about timeouts due to long postback times, etc. 该过程允许用户在必要时继续进行其他工作,而不必担心由于回发时间长等原因导致的超时。

For your specific scenario, you could add a GUID property to the Job class (which would be passed back to the client). 对于您的特定方案,您可以将GUID属性添加到Job类(这将传递回客户端)。

When Execute is complete, you could add this GUID to a static collection (ie Dictionary<Guid, string> ) which the ajax request would check (the string value could store status or completion information). 当Execute完成时,您可以将此GUID添加到ajax请求将检查的静态集合(即Dictionary<Guid, string> )中(字符串值可以存储状态或完成信息)。

When the ajax request fires, it would check this static collection and, when it finds its job, remove it and return the value to the caller. 当ajax请求触发时,它将检查此静态集合,并在找到其作业时将其删除,并将该值返回给调用方。

尝试使用全局变量(静态)或通过Execute()方法引发事件。

You can create a static property that is updated by your Execute method and bind the Text property of the Label to the static property in the aspx's OnInit method, Label.Text = Job.StaticProperty, if you need a somewhat dynamic response you could use the Ajax Timer Control to call a method on the aspx page to return the same static value from the aspx Page. 您可以创建一个由Execute方法更新的静态属性,并将Label的Text属性绑定到aspx的OnInit方法中的静态属性Label.Text = Job.StaticProperty,如果您需要某种动态响应,则可以使用Ajax Timer Control调用aspx页面上的方法以从aspx页面返回相同的静态值。

public static class Job 
{ 
    public static string UpdatedValue { get; private set; } // Or whatever the property is you wish to expose.

    // The Execute method is called by a scheduler and must therefore  
    // have this exact signature (i.e. it cannot take any paramters). 
    public static string Execute() 
    { 
        // Do work
        Job.UpdatedValue = "Execute Completed";
    } 
} 


protected override OnInit(EventArgs e)
{
    base.OnInit(e);
    this.TextLabel.Text = Job.UpdatedValue;
}


// Using MSDN basic sample
protected void Timer1_Tick(object sender, EventArgs e)
{
    this.TextLabel.Text = Job.UpdatedValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM