私は ScriptManager の提案をいじりました-最終的には機能するようになったと思いますが、タイマーのアイデアは実装が簡単で、実際には(!)それほどハックではないようです?!
最初のページのレンダリングが完了した後にパネルを更新する方法は次のとおりです...
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAXPostLoadCall._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<h2>And now for a magic trick...</h2>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick" />
<asp:Label ID="Label1" runat="server">Something magic is about to happen...</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
default.aspx.csの背後にあるコードは読み取ります
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace AJAXPostLoadCall
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void DoMagic()
{
Label1.Text = "Abracadabra";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
// Do the magic, then disable the timer
DoMagic();
Timer1.Enabled = false;
}
}
}
そのため、ページが読み込まれ、ページが読み込まれてから 2 秒後にタイマー (UpdatePanel に含まれる) が起動します (タイマーが実際にいつ開始されるかわかりません)。ラベル テキストが書き換えられ、それ以上の更新を停止するためにタイマーが無効になります。
十分に単純ですが、これが恐ろしいハックであるかどうかを教えてもらえますか?