簡體   English   中英

如何獲取ASP.NET page_load事件以觸發AJAX UpdatePanel更新

[英]How to get an ASP.NET page_load event to trigger AJAX UpdatePanel update

我在ASP.NET中有一個頁面,上面有一些updatepanel。 頁面加載后,我希望updatepanels觸發updatepanels更新。

我正在使用這個答案,說這個Javascript在頁面上。

$(document).ready(function () {
    window.__doPostBack('<%= hiddenAsyncTrigger.ClientID %>', 'OnClick');
});

但是,這會導致click事件被垃圾郵件,因為load事件正在觸發antoher load ...

更新面板都運行正常,問題是在頁面加載后觸發更新一次

使用客戶端控件而不是服務器端:

<%@ Page Language="C#" %>
<script runat="server">
  protected string CurrentTime()
  {
    return DateTime.Now.ToString();
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title>Untitled Page</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script language="JavaScript">
      $(document).ready(function () {
        $("#Updater").click()
      });
    </script>
  </head>

  <body>
    <form id="MyForm" runat="server">
      <div>
        Outside the panel: <%=CurrentTime() %>
      </div>
      <br />
      <br />
      <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />
      <asp:UpdatePanel ID="panel" runat="server" UpdateMode="conditional">
        <ContentTemplate>
          <div>
            Inside the panel: <%=CurrentTime() %>
          </div>
          <input type="submit" id="Updater" style="display: none" />
        </ContentTemplate>
      </asp:UpdatePanel>
    </form>
  </body>
</html>

另一種解決方案 - 沒有隱藏觸發器,使用__doPostBack

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script language="JavaScript">
        $(document).ready(function () {
            __doPostBack('panel', '');
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Outside the panel: <%=CurrentTime() %>
        </div>
        <br />
        <br />
        <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />
        <asp:UpdatePanel ID="panel" runat="server" UpdateMode="conditional">
            <ContentTemplate>
                <div>
                    Inside the panel: <%=CurrentTime() %>
                </div>
                <input type="submit" id="Updater" style="display: none" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected string CurrentTime()
        {
            System.Diagnostics.Debug.WriteLine("CurrentTime was called");
            return DateTime.Now.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("PageLoad was called");
        }
    }
}

使用Javascript觸發更新面板中的隱藏按鈕,結束解決此問題

JS

var timer;
var polling_interval = 30000;

$(document).ready(function () {
    TriggerUpdatePanelUpdate();// update as soon as DOM is ready
    StartPolling();
});

function StartPolling() {
    PollLoop();
}

function StopPolling() {
    clearTimeout(timer);
}

function PollLoop() {
    timer = setTimeout(
        function() {
            TriggerUpdatePanelUpdate();
            PollLoop();
        },
        polling_interval 
    );
}

function TriggerUpdatePanelUpdate() {
    document.getElementById('hiddenAsyncTrigger').click();
}

HTML

<asp:Button ID="hiddenAsyncTrigger" runat="server" OnClick="DoWork" Text="AsyncUpdate" style="display: none;"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM