Tuesday, November 6, 2007

Ajax UpdatePanel (17) pageLoading Event

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RandomRefreshControl.ascx.cs" Inherits="RandomRefreshControl" %>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<%= DateTime.Now %>

</ContentTemplate>

</asp:UpdatePanel>



using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class RandomRefreshControl : System.Web.UI.UserControl

{

private static Random random = new Random(DateTime.Now.Millisecond);

protected void Page_Load(object sender, EventArgs e)

{

if (random.NextDouble() > 0.5)

{

this.UpdatePanel1.Update();

}

}

}



<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="3_HighlightRefreshedPanels.aspx.cs" Inherits="_3_HighlightRefreshedPanels" Title="HighlightRefreshedPanels" %>

<%@ Register Src="RandomRefreshControl.ascx" TagName="RandomRefreshControl" TagPrefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<uc1:RandomRefreshControl id="RandomRefreshControl1" runat="server" />

<br />

<uc1:RandomRefreshControl id="RandomRefreshControl2" runat="server" />

<br />

<uc1:RandomRefreshControl id="RandomRefreshControl3" runat="server" />

<br />

<uc1:RandomRefreshControl id="RandomRefreshControl4" runat="server" />

<br />

<uc1:RandomRefreshControl id="RandomRefreshControl5" runat="server" />

<br />

<asp:Button ID="Button1" runat="server" Text="Button" />

<script language="javascript" type="text/javascript">

function highlightPanels(panels, clear)

{

for (var i = 0; i < panels.length; i++)

{

var panel = panels[i];

panel.style.border = clear ? "solid 0px white" : "solid 2px red";

panel.style.backgroundColor = clear ? "white" : "#d6dde8";

}

}

Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(

function(sender, e)

{

var panelsUpdating = Array.clone(e.get_panelsUpdating());

highlightPanels(panelsUpdating);

window.setTimeout(

function(){ highlightPanels(panelsUpdating, true); },

2000);

});

</script>

</asp:Content>



using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _3_HighlightRefreshedPanels : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.Button1);

}

}

In this code, we have 5 user control. When Button1 is clicked, we don't know which ones will be updated, so we can highlight the ones that are updated when Button1 is clicked.

blog comments powered by Disqus