Analytics

2012年5月15日 星期二

[ASP.NET]MasterPage裡加入iFrame連結網頁,並由連結網頁與MasterPage連動(MasterPage was added iFrame links page by page with links MasterPage linked)


問題
當MasterPage裡有設到ajax Timer的元件時,會發現整個頁面發生postback,因此為了減緩此現象,可以在MasterPage加入iFrame,讓另一個網頁取代Timer的動作,因此MasterPage就不會有Postback的現象




解決方法
以下範例示範動態server時間與從另一頁面促發masterpage事件

Site.aspx(放入以下區塊)
<div class="main">
<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>
Site.Master.cs
using System;
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();
        }
    }
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FramesetDemo.WebForm1" %>

<!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>
WebForm1.cs
using System;
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");
        }
    }
}
WebForm2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="FramesetDemo.WebForm2" %>

<!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>
參考: http://forums.asp.net/t/1162285.aspx

沒有留言:

熱門文章