Wednesday, October 31, 2007

Ajax UpdatePanel (15) initializeRequest Event

Site.Master

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">

</asp:ScriptManager>

<div>

<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

</asp:contentplaceholder>

</div>

<br />

<div id="message" style="color:Red;"></div>

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

var timeoutSeed = null;

function showMessage(message, timeout)

{

$get("message").innerHTML = message;

if (timeoutSeed)

{

window.clearTimeout(timeoutSeed);

}

timeoutSeed = window.setTimeout(

function(){ $get("message").innerHTML = ""; },

timeout 2500);

}

</script>

</form>

1_PrecedencePostBack.aspx


<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="1_PrecedencePostBack.aspx.cs" Inherits="_1_PrecedencePostBack" Title="PrecedencePostBack" %>

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

<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="0" DynamicLayout="false">

<ProgressTemplate>

<span style="color:Red">Loading...</span>

</ProgressTemplate>

</asp:UpdateProgress>

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

<ContentTemplate>

<%= DateTime.Now %><br />

<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />

<asp:Button ID="btnPrecedence" runat="server" Text="Precedence" OnClick="Button_OnClick" />

<asp:Button ID="btnNormal" runat="server" Text="Normal" OnClick="Button_OnClick" />

</ContentTemplate>

</asp:UpdatePanel>

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

var lastPostBackButtonId = null;

var btnPrecedenceId = "<%= this.btnPrecedence.ClientID %>";

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(

function(sender, e)

{

var prm = Sys.WebForms.PageRequestManager.getInstance();

if (prm.get_isInAsyncPostBack())

{

if (lastPostBackButtonId == btnPrecedenceId)

{

e.set_cancel(true);

if (e.get_postBackElement().id == btnPrecedenceId)

{

showMessage("you cannot click btnPrecedence more than once.");

}

else

{

showMessage("please wait for the btnPrecedence to finish.");

}

return;

}

else if (e.get_postBackElement().id == btnPrecedenceId)

{

showMessage("you've clicked the btnPrecedence button, the btnNormal request will be cancelled.");

}

else

{

showMessage("you've clicked the btnNormal button more than once, only the last request will be processed.");

}

}

lastPostBackButtonId = e.get_postBackElement().id;

});

</script>

</asp:Content>

1_PrecedencePostBack.aspx



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;

using System.Threading;

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

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button_OnClick(object sender, EventArgs e)

{

Thread.Sleep(3000);

Button button = sender as Button;

this.Label1.Text = String.Format("you've clicked ''{0}'' button", button.Text);

}

}

This code uses initializeRequest event to disallow user to click button more than once, or based on the priority to determine which button's request should be processed.

blog comments powered by Disqus