简体   繁体   English

页面加载后刷新标签

[英]refresh label after page load

I have a function which is taking a lot of time in a web application. 我有一个在Web应用程序中花费大量时间的功能。 So i decided to create a thread for it. 因此,我决定为其创建一个线程。

Thread t = new Thread(getEventErrors);
            t.Start();

The function calculates a value and applies it in a label. 该函数计算一个值并将其应用到标签中。

This is an issue because in asp.net the page loads once and the label is not updated till then. 这是一个问题,因为在asp.net中该页面仅加载一次,并且标签直到那时都不会更新。

how do i implement this using the AJAX? 我如何使用AJAX来实现呢?

Is there a way in which the page is loaded and after the value is calculated the label gets updated? 有没有一种方法可以加载页面,并在计算出值后更新标签?

Any sample code will be highly appreciated. 任何示例代码将不胜感激。

Thanks 谢谢

 private void getEventErrors()
    {         
        EventLog eventLog = new EventLog("Application", ".");

        getEvents(eventLog.Entries);
    }

 private void getEvents(EventLogEntryCollection eventLogEntryCollection)
    {
        int errorEvents = 0;

        foreach (EventLogEntry logEntry in eventLogEntryCollection)
        {
            if (logEntry.Source.Equals("Application Name"))
            {
                DateTime variable = Convert.ToDateTime(logEntry.TimeWritten);
                long eventTimeTicks = (variable.Ticks);
                long eventTimeUTC = (eventTimeTicks - 621355968000000000) / 10000000;

                long presentDayTicks = DateTime.Now.Ticks;
                long daysBackSeconds = ((presentDayTicks - 864000000000) - 621355968000000000) / 10000000;


                if (eventTimeUTC > daysBackSeconds)
                {
                    if (logEntry.EntryType.ToString() == "Error")
                    {
                        errorEvents = errorEvents + 1;
                    }                        
                }
            }
        }
        btn_Link_Event_Errors_Val.Text = errorEvents.ToString(GUIUtility.TWO_DECIMAL_PT_FORMAT);            
        if (errorEvents == 0)
        {
            lbl_EventErrorColor.Attributes.Clear();
            lbl_EventErrorColor.Attributes.Add("class", "green");
        }
        else
        {
            lbl_EventErrorColor.Attributes.Clear();
            lbl_EventErrorColor.Attributes.Add("class", "red");
        }            
    }

this is the code.. but i want to handle all the long time consuming code using ajax. 这是代码..但我想使用ajax处理所有耗时的代码。

For example the page should load fast and the long function should keep loading in the thread with a wait cursor, when the value is got it gets displayed in the label or any other control. 例如,页面应该快速加载,而long函数应该使用等待游标继续加载到线程中,当获得该值时,它将显示在标签或任何其他控件中。

There is no way for the server to send some data after the response has been sent with an "update". 在发送带有“更新”的响应之后,服务器无法发送一些数据。 A spontaneity request sent by the server to a client is just going to be ignored. 服务器发送给客户端的自发请求将被忽略。

Your client will need to send a request to the server after the page is loaded (meaning JavaScript code) to request additional data that you can then update the page with. 页面加载后(即JavaScript代码),您的客户端需要向服务器发送请求,以请求其他数据,然后可以使用这些数据更新页面。

The best method that I can think of would be to make a web service/web method with the data in the form, or to make an ICallbackEventHandler (I rather like working with these) which allows you to request the data from some JavaScript method. 我能想到的最好的方法是使用表单中的数据制作一个Web服务/ Web方法,或者制作一个ICallbackEventHandler (我更喜欢使用它们)来允许您从某些JavaScript方法中请求数据。 Now at this point you can either setup these web method to calculate just this piece of data when asked, or you can spin up the thread like you're doing and use the session. 现在,您可以设置这些Web方法以仅在被询问时计算此数据,也可以像执行操作一样旋转线程并使用会话。 This would mean having the other thread calculate the data and store it in session on page load, and then have the JavaScript method get the value of out of the session. 这意味着让另一个线程计算数据并将其在页面加载时存储在会话中,然后让JavaScript方法从会话中获取值。 The latter method is a bit more fragile. 后一种方法比较脆弱。 (If they hit the page several times in different browsers/tabs at the same time, for example.) On top of that, if the session value hasn't been populated yet you'll either need to block the thread (bad idea) or have the JavaScript method poll repeatedly until the session value is populated. (例如,如果它们同时在不同的浏览器/选项卡中多次单击该页面。)此外,如果尚未填充会话值,则您需要阻止该线程(不好的主意)或让JavaScript方法反复轮询,直到填充会话值为止。

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

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