我要在按钮上执行多个存储过程,单击我的.aspx页面上的事件,每次过程执行时,我必须在Process上显示一条消息
流程1已经完成。过程2已经完成。过程3已经完成。
请帮忙
发布于 2014-08-13 08:46:35
一种方法是结合Ajax计时器使用Update。
下面这篇文章可能会很有用。
http://msdn.microsoft.com/en-in/library/cc295400.aspx
本文中给出的示例向您展示了如何用计时器事件的当前服务器时间更新update中的标签。您可以轻松地调整代码以满足您的需要,并显示您想要显示的任何消息,而不是服务器时间。
编辑
按照要求,这里有一些示例代码。
DoWorkAsync方法以满足您的需要。ASPX文件:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test2.aspx.vb" Inherits="Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Asynchronous Update Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" ID="Timer1" Interval="1000" />
<asp:Button ID="Button1" runat="server" Text="Start" /><br />
<asp:Label runat="server" Text="Click Start button to being processing." ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>文件后面的代码:
(注意内联评论)
Partial Class Test2
Inherits System.Web.UI.Page
Private MyAsyncProcessor As AsyncProcessor
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("MyAsyncProcessor") IsNot Nothing Then
' we are still processing.. so extract the reference of our class back from session variable
Me.MyAsyncProcessor = Session("MyAsyncProcessor")
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' this event is fired everytime a timer ticks.
If MyAsyncProcessor IsNot Nothing Then
Label1.Text = MyAsyncProcessor.StatusMessage
If Not MyAsyncProcessor.IsProcessing Then
MyAsyncProcessor = Nothing
Session.Remove("MyAsyncProcessor")
Timer1.Enabled = False
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Button1.Enabled = False
Timer1.Enabled = True
MyAsyncProcessor = New AsyncProcessor
MyAsyncProcessor.Start()
Session("MyAsyncProcessor") = MyAsyncProcessor 'save reference in a session variable
End Sub
End Class
Public Class AsyncProcessor
Public IsProcessing As Boolean
Public StatusMessage As String
Public Sub Start()
Dim th As New Threading.Thread(AddressOf DoWorkAsync)
th.Start()
End Sub
Private Sub DoWorkAsync()
' *** Replace this code with whatever work you want to do ***
' Set IsProcessing=True at beginnning of task and set it to False when exitting.
' Update the StatusMessage at regular intervals.
IsProcessing = True
For i = 1 To 10
'' this sleep is only to simulate a long going task
Threading.Thread.Sleep(1000)
StatusMessage &= String.Format("Task #{0} completed... <br>", i)
Next
StatusMessage &= "All tasks completed."
IsProcessing = False
End Sub
End Class发布于 2014-08-13 09:10:05
另一种方法是将工作委托给iframe中的页面。在iframe页面上,当您的代码被处理时,您可以更新一个会话变量以包含“已完成的处理第一步”(或其他任何内容),并使用父页面上的PageMethods调用一个Web方法,该方法获取由iframe中页面上的代码更新的会话的值。
https://stackoverflow.com/questions/25280053
复制相似问题