問題
當MasterPage裡有設到ajax Timer的元件時,會發現整個頁面發生postback,因此為了減緩此現象,可以在MasterPage加入iFrame,讓另一個網頁取代Timer的動作,因此MasterPage就不會有Postback的現象
解決方法
以下範例示範動態server時間與從另一頁面促發masterpage事件
Site.aspx(放入以下區塊)
<div class="main">Site.Master.cs
<asp:Label Text="時間表" runat="server"></asp:Label>
<iframe NAME="bottom" FRAMEBORDER="0" width="100%" style="height: 50px" src="../WebForm1.aspx"></iframe>
<asp:Label ID="Label1" Text="觸發主頁面" runat="server"></asp:Label>
<iframe NAME="bottom" FRAMEBORDER="0" width="100%" style="height: 50px" src="../WebForm2.aspx"></iframe>
<div style="visibility:hidden">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</div>
using System;WebForm1.aspx
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FramesetDemo
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = "您按下按鈕時間" + DateTime.Now.ToString();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FramesetDemo.WebForm1" %>WebForm1.cs
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;WebForm2.aspx
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FramesetDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
this.Label1.Text = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="FramesetDemo.WebForm2" %>參考: http://forums.asp.net/t/1162285.aspx
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="javascript:window.parent.document.getElementById('Button1').click();" name="Button1" value="Button" id="Button1" /></div>
</div>
</form>
</body>
</html>
沒有留言:
張貼留言