Adding event handlers for events raised by the PageRequestManager in .NET

Encoder PDF-417 2d barcode in .NET Adding event handlers for events raised by the PageRequestManager

Listing 7.6 Adding event handlers for events raised by the PageRequestManager
Generating PDF 417 In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
QR-Code Encoder In VS .NET
Using Barcode creation for ASP.NET Control to generate, create QR image in ASP.NET applications.
www.OnBarcode.com
with(Sys.WebForms.PageRequestManager.getInstance()) { add_initializeRequest(onInitializeRequest); add_beginRequest(onBeginRequest); add_pageLoading(onPageLoading); add_pageLoaded(onPageLoaded); add_endRequest(onEndRequest ); } function onInitializeRequest(sender, args){ var row = createEventRow("initializeRequest", ""); $get('clientEvents').appendChild(row); }
Barcode Generation In .NET
Using Barcode generator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Print Data Matrix 2d Barcode In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
Add handlers for PageRequestManager events
Generate 2D Barcode In .NET
Using Barcode creation for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
Code-128 Generation In .NET
Using Barcode printer for ASP.NET Control to generate, create Code 128B image in ASP.NET applications.
www.OnBarcode.com
Under the hood of the UpdatePanel
PDF 417 Encoder In VS .NET
Using Barcode maker for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
www.OnBarcode.com
Universal Product Code Version E Printer In .NET
Using Barcode creation for ASP.NET Control to generate, create UPC-E Supplement 2 image in ASP.NET applications.
www.OnBarcode.com
function onBeginRequest(sender, args){ var row = createEventRow("beginRequest", ""); $get('clientEvents').appendChild(row); } function onPageLoading(sender, args){ var row = createEventRow("pageLoading", ""); $get('clientEvents').appendChild(row); } function onPageLoaded(sender, args){ var row = createEventRow("pageLoaded", ""); $get('clientEvents').appendChild(row); } function onEndRequest(sender, args){ var row = createEventRow("endRequest", $get('clientEvents').appendChild(row); }
PDF 417 Creation In None
Using Barcode printer for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
PDF417 Maker In Java
Using Barcode printer for Java Control to generate, create PDF-417 2d barcode image in Java applications.
www.OnBarcode.com
"");
Decoding Barcode In Java
Using Barcode Control SDK for Eclipse BIRT Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
EAN-13 Supplement 5 Encoder In None
Using Barcode creation for Word Control to generate, create GS1 - 13 image in Word applications.
www.OnBarcode.com
NOTE
PDF-417 2d Barcode Printer In .NET Framework
Using Barcode generator for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
EAN 128 Drawer In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create GS1-128 image in Visual Studio .NET applications.
www.OnBarcode.com
There can be only one PageRequestManager on a page. In order to work with the object, you must retrieve an instance of it. You do so by calling the object s static getInstance method. To cut back on some typing, we used the JavaScript with statement to leverage the same instance for all the commands that add the handlers.
UPC Symbol Generation In Objective-C
Using Barcode drawer for iPhone Control to generate, create GTIN - 12 image in iPhone applications.
www.OnBarcode.com
Making UPC Code In Java
Using Barcode creator for Android Control to generate, create UPCA image in Android applications.
www.OnBarcode.com
The handlers are in place; now you need events to initiate a partial-page update. To keep things simple, add an UpdatePanel to the page and assign it some child controls that invoke a postback when you interact with them (see listing 7.7).
PDF 417 Creator In None
Using Barcode maker for Office Word Control to generate, create PDF417 image in Microsoft Word applications.
www.OnBarcode.com
Barcode Recognizer In C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Listing 7.7 Adding an UpdatePanel control and child controls to invoke asynchronous postbacks
Recognize Barcode In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Drawing EAN128 In Java
Using Barcode generator for Java Control to generate, create EAN 128 image in Java applications.
www.OnBarcode.com
<asp:Panel ID="Panel1" runat="server" GroupingText="UpdatePanel1"> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> Last Updated: <asp:Label ID="LastUpdated1" runat="server" /> <div> <asp:Button ID="Update1" runat="server" Text="Update" OnClick="Update_Click" /> </div> </ContentTemplate> </asp:UpdatePanel> </asp:Panel>
A client-side event viewer
You add a Label control and a Button control as the child controls for the UpdatePanel. When the button is clicked, the text in the label is updated with the current time (a pattern used throughout the book). When the time changes, this serves as the indication that a partial update has been applied:
protected void Update_Click(object sender, EventArgs e) { LastUpdated1.Text = DateTime.Now.ToLongTimeString(); }
Because the button is declaratively placed in the ContentTemplate tag of the UpdatePanel, it performs a partial postback when you interact with it. This time, when you run the application and click the Update button, the events raised by the PageRequestManager and Application object are displayed in the event viewer application. Figure 7.5 proudly displays your progress. Notice how the distinction between the types of postbacks is made during the load event. Let s evaluate where you are right now before going any further. You have an application that registers a handler for each of the events raised during a partial postback. We ve started exploring some of the arguments passed in by initially adding more logic in the load event handler. It makes sense to continue our investigation with the first event raised by the PageRequestManager: the initializeRequest event. This important occasion is when you can abort a request or determine which request has precedence over another. Let s continue to build out the application, resuming with how to abort a request.
Your progress thus far: capturing each event raised during an asynchronous postback
Under the hood of the UpdatePanel
Aborting a postback
The initializeRequest event is raised to signify the early stages of an asynchronous request. The arguments passed along with the event are of the type InitializeRequestEventArgs. Here, you can retrieve the ID of the element that invoked the request by examining its postBackElement member. In addition, based on the ID, you may decide to abort a request by calling the PageRequestManager s abortPostBack method. To see this in action, let s add another button to the UpdatePanel that will give you the opportunity to abort a request. In addition, you ll delay the logic on the server to increase the window of opportunity for aborting when the Update button is clicked. Both changes to the declarative markup and the code-behind are shown in listing 7.8.
Listing 7.8 Clicking the Abort button aborts the postback.
protected void Update_Click(object sender, EventArgs e) { LastUpdated1.Text = DateTime.Now.ToLongTimeString(); System.Threading.Thread.Sleep(5000); } function onInitializeRequest(sender, args){ var details = "postBackElementID = " + args.get_postBackElement().id;
Slow down update
Abort request
if (args.get_postBackElement().id == "Abort") Sys.WebForms.PageRequestManager.getInstance().abortPostBack(); var row = createEventRow("initializeRequest", details); $get('clientEvents').appendChild(row); }
First, you put a B Sleep call into the code-behind to slow down the update, essentially giving you a 5-second window (5,000 milliseconds) of opportunity to test the abort logic. Remember, calling Sleep is only for demonstration purposes and should never be implemented in production code. Next, you compare the ID of the element that initiated the postback with that of the Abort button on the page; if they match, you call the C abortPostBack function in the PageRequestManager. The result is a premature end to the request.
Copyright © OnBarcode.com . All rights reserved.