【问题标题】:VB.Net "logger" asp:Table and asp:UpdatePanel dynamic asynchronous updateVB.Net "logger" asp:Table 和 asp:UpdatePanel 动态异步更新
【发布时间】:2015-07-24 20:55:12
【问题描述】:

我正在用 ASP.Net 编写网页。目前,我有一个asp:Table,用作处理输出的一种“日志”。这个想法是用户选择几个文件并单击一个按钮,每个文件都被“处理”,日志显示正在发生的事情。处理异步进行。

这里是相关的处理段:

    Protected Sub DoAsyncWork()

    Dim count = 0
    For Each row As GridViewRow In gvList.Rows
        count = count + 1
        If CType(row.FindControl("cbImport"), System.Web.UI.WebControls.CheckBox).Checked Then

            push_to_log("")
            push_to_log("Updating Active Projects +" + HttpUtility.HtmlDecode(row.Cells(1).Text).ToString.Substring(0, 30) + "...")

            Dim xp(3) As Object
            xp(0) = HttpUtility.HtmlDecode(row.Cells(0).Text)
            xp(1) = HttpUtility.HtmlDecode(row.Cells(1).Text)
            xp(2) = HttpUtility.HtmlDecode(row.Cells(2).Text)
            xp(3) = 0
            'oDC.UpdateData("Import_P3e_Project ", xp)
            If (xp(3) <> 0) Then
                push_to_log("Success: " + xp(3).ToString + " have been updated")
            Else
                push_to_log("Failure: " + xp(3).ToString + " activities updated")
            End If

        End If


    Next
    push_to_log("")
    push_to_log("Import Complete!")

End Sub

这就是我调用进程工作者函数的方式:

Protected Sub button_Import(sender As Object, e As EventArgs) Handles btnImport.Click
    Dim t As New Thread(New ThreadStart(AddressOf DoAsyncWork))
    t.Priority = Threading.ThreadPriority.Normal
    t.Start()
    push_to_log("Start Import")
End Sub

我将内容附加到日志的方式是动态创建行和单元格,然后将它们添加到我的表中。下面是相关的子程序:

Protected Sub push_to_log(ByVal str As String)

    Dim newRow As TableRow = New TableRow
    Dim newCell As TableCell = New TableCell

    logArrayList.Add(str)
    Me.ViewState.Add("arrayListInViewState", logArrayList)

    newCell.Text = str
    newCell.Style("Color") = "White"
    newCell.ID = "cell" + (logArrayList.Count - 1).ToString
    newRow.ID = "row" + (logArrayList.Count - 1).ToString

    newRow.Cells.Add(newCell)

    logTable.Rows.Add(newRow)

    HiddenButton_Click(HiddenButton, New EventArgs())
    'UpdateLogPanel.Update()
    'UpdateLogPanel.Focus()
End Sub

通过使用 ViewState 存储我的数据数组列表并在回发时重新创建日志,我已经正确地保留了日志。我的日志的相关标记如下所示:

<asp:UpdatePanel ID="UpdateLogPanel" UpdateMode="Conditional" runat="server">
          <Triggers>
            <asp:AsyncPostBackTrigger ControlID="HiddenButton" />
        </Triggers>
          <ContentTemplate>
    <div ID="Div1" class="DefinitionPanel" style="text-align:left;height:200px;overflow:hidden;" runat="server">


       <span style="display:inline-block; width:100px;"></span>
       <div class="scrollingtable">
          <div>
            <div id="viewContainer">
              <asp:table id="logTable" runat="server" enableviewstate="false">

              </asp:table>
            </div>
          </div>
        </div>
    </div>
        <asp:Button ID="HiddenButton" runat="server" style="display:none;" />
          </ContentTemplate>
    </asp:UpdatePanel>

我正在尝试让我的asp:Table 每次发布消息时更新。我认为启用部分回发并使用UpdatePanel 将是正确的解决方案,但在整个过程完成之前,我的日志仍然没有输出任何内容。

在我的asp:Table/log 中添加消息后,我尝试调用

UpdateLogPanel.Update() 

这似乎没有什么不同。最后我尝试添加asp:AsyncPostBackTrigger 和隐藏按钮,希望它能解决问题,但似乎没有。以下是hiddenButton 事件的样子:

Protected Sub HiddenButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles HiddenButton.Click
    UpdateLogPanel.Visible = True

End Sub

任何关于如何在我向其添加消息时使我的日志重新呈现的指导都将受到高度重视。

【问题讨论】:

  • VB6.NET?有趣的。编辑时间...

标签: asp.net vb.net logging updatepanel


【解决方案1】:

我选择使用池而不是异步方法来实现。

我仍然使用 asp:UpdatePanel。我现在使用的是网格视图和计时器,而不是 UpdatePanel 中的表格。当计时器触发时,我重新绑定 gridview,从而显示任何新内容

完成这项工作的关键组件:

将父更新面板和母版页放在一起:

Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    'set reference to master site page
    mstr = CType(Master, Site)

    'setup partial rendering so Log can update asynchronously
    scriptManager = CType(mstr.FindControl("ScriptManager1"), ScriptManager)
    scriptManager.EnablePartialRendering = True
    scriptManager.AsyncPostBackTimeout = 28800
    CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).UpdateMode = UpdatePanelUpdateMode.Conditional
    CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).ChildrenAsTriggers = False
End Sub

UpdatePanel 的标记看起来像

    <asp:UpdatePanel ID="UpdateLogPanel" UpdateMode="Conditional" 
            RenderMode="Inline" ChildrenAsTriggers="false" runat="server">
                  <ContentTemplate>
                       <%--The Gridview and other Hidden Fields--%>
                       <asp:Timer ID="myTimer" OnTick="timer_tick" runat="server" Interval="1000" Enabled="false"/>
                  </ContentTemplate>
                  <Triggers>
                      <asp:AsyncPostBackTrigger ControlID="myTimer" EventName="Tick" />
                  </Triggers>
       </asp:UpdatePanel> 

触发日志机制只需通过设置 myTimer.Enabled = True

timer_tick 事件看起来像

Public Sub timer_tick(ByVal sender As Object, ByVal e As EventArgs)
    generate_log() 

    //other logging logic (increment counters, "timeout" mechanism)

    UpdateLogPanel.Update()

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多