0
我有一個包含許多PDF的文件夾。當用戶從DropDownList
中選擇類別並單擊按鈕時,系統將文件(不是全部)複製到新文件夾中,然後生成壓縮文件。AsyncCallback功能不起作用
這些步驟是可以的,但問題是生成的zip服務完成時。
我打算寫一個例子:
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="Server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
<asp:ServiceReference Path="SomeService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManagerProxy>
<script type="text/javascript">
function copyFiles(id) {
SomeService.CopyFiles($ja("#DropDownList1").val(), id, FuncionSucceeded, FuncionFailed);
}
function FuncionSucceeded(result, eventArgs) {
document.getElementById("GenerateZip").click();
}
function FuncionFailed(result, eventArgs) {
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Generate Zip" />
<asp:Timer ID="Timer1" runat="server" Interval="5000" Enabled="false">
</asp:Timer>
<asp:Button ID="btnGenerateZip" runat="server" Style="display: none;" ClientIDMode="Static" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif"
Visible="False" />
<asp:Label ID="Label1" runat="server" Text="" Visible="false"></asp:Label>
<asp:LinkButton runat="server" Visible="false" ID="linkzip" Text="Descargar" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
,代碼:
'This code call the client side to copy the files
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
If DropDownList1.SelectedValue = "0" Then
Exit Sub
End If
Dim pathName As String = "The path here"
If Not File.Exists(pathName) Then
Dim folder As String = Path.GetDirectoryName(pathName)
If Not Directory.Exists(folder) Then
Directory.CreateDirectory(folder)
End If
End If
Image1.Visible = True
Label1.Visible = True
Timer1.Enabled = True
Label1.Text = "Generating zip"
Dim script As String = "copyFiles('1234');"
ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), Guid.NewGuid().ToString(), script, True)
End Sub
'When the client side returns the function `FunctionSucceeded` call next function
Private Sub btnGenerateZip_Click(sender As Object, e As EventArgs) Handles btnGenerarZip.Click
GenerateZip()
End Sub
Private Sub GenerateZip()
Try
Dim ws As New AnotherService.ServiceExample
Dim ar2 As New AsyncCallback(AddressOf DownloadFile)
Dim pathName As String = String.Format("{0}", path)
result = ws.BeginSomeFunction(path, ar2, ws)
Catch ex As Exception
generaZip = False
End Try
End Sub
而且回調函數:
Protected Sub DownloadFile(ar As IAsyncResult)
Dim ws As New AnotherService.ServiceExample
Try
Dim pathName As String ="Path"
ws.deleteFolder(pathName)
ws.EndSomeFunction(ar)
Image1.Visible = False
Label1.Text = "OK"
Timer1.Enabled = Image1.Visible
linkzip.Visible = True
generaZip = True
Catch ex As Exception
generaZip = False
End Try
End Sub
和計時器滴答:
Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Select Case generaZip
Case True
Image1.Visible = False
Label1.Text = "Everything is ok"
Timer1.Enabled = Image1.Visible
linkzip.Visible = True
Case False
Image1.Visible = False
Label1.Text = "Error"
Timer1.Enabled = Image1.Visible
End Select
End Sub
問題是當我們在DownloadFile
函數(異步回調函數),我在那裏沒有任何工作。我改變了許多屬性,值,變量值,當tick函數觸發時,就像我沒有做任何事情。
我已經改變了所有這些行:
Image1.Visible = False
Label1.Text = "OK."
Timer1.Enabled = Image1.Visible
linkzip.Visible = True
generaZip = True
你的意思是generaZip仍然是假的?這是在哪裏宣佈的,我們確定它獲得/保留它的價值? –
yes仍然是false,image1.visible仍然是true,以及所有其他屬性。 @JimmySmith 在頁面頂部聲明並且在不是回發時初始化 – Ary
我不確定它是否會以這種方式正確保留此True值。如果你創建一個會話變量並將True分配給它,會發生什麼? 'Select Case Session(「generaZip)' –