Common Partial Postback
Common_PartialPostback_VBNet\RegisterDataItemDemo.aspx.vb
' Copyright 2011 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Public Partial Class RegisterDataItemDemo
  Inherits System.Web.UI.Page
  #Region "ASP.NET Page Life Cycle Event Handlers"

  Protected Sub Page_Load(ByVal sender As Object, ByVal eventArgs As System.EventArgs)
    ' Register the Zoom To Point button and Zoom To Menu to issue postbacks asynchronously.  Note that
    ' controls registered in this way must implement either INamingContainer, IPostBackDataHandler, or 
    ' IPostBackEventHandler
    ScriptManager1.RegisterAsyncPostBackControl(Button1)
    ScriptManager1.RegisterAsyncPostBackControl(Menu1)
  End Sub

  Protected Sub Page_PreRender(ByVal sender As Object, ByVal eventArgs As System.EventArgs)
    Dim scriptKeyCustom As String = "customDataItemScript"
    ' Check whether a script block with the name stored in scriptKeyCustom has already
    ' been registered on the client, and whether the page is in an asynchronous postback.
    ' If neither of these is true, create and register the script block.  Note that replacing
    ' ScriptManager1.IsInAsyncPostBack with Page.IsPostback will work initially, but if a
    ' full page postback occurs, the script may be lost.
    If (Not Me.Page.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), scriptKeyCustom)) AndAlso (Not ScriptManager1.IsInAsyncPostBack) Then
      ' Construct the JavaScript block that will be responsible for processing data items.
      ' 
      ' onLoadFunction specifies AsyncResponseHandler as a handler for the pageLoading AJAX
      ' client-side event.  This event fires during asynchronous postbacks after the response 
      ' has been received from the server, but before any content on the page is updated.  
      ' 
      ' AsyncResponseHandler retrieves the data items thate were registered server-side during 
      ' the asynchronous postback by accessing the dataItems property on the second argument 
      ' passed to the handler.  It then gets the particular data item corresponding to the 
      ' page by passing the page's client ID to the dataItems array as an array index.  This 
      ' data item, assumed to be formatted as a Web ADF callback result, is then passed to 
      ' ESRI.ADF.System.processCallbackResult - the client-side Web ADF function responsible 
      ' for parsing callback results and updating Web ADF controls accordingly.
      '
      ' Below the function declarations, onLoadFunction is added as a handler for the AJAX 
      ' client-side event init, which is raised one time when the page is first rendered. 
      ' This is therefore the appropriate place for onLoadFunction to be called, since the 
      ' asynchronous pageLoading handler in this case can remain unchanged for the life
      ' of the application.
      '
      ' The functions are enclosed in an extra pair of curly braces to allow the subsequent
      ' call to String.Format.  String.Format is designed to replace the contents of curly
      ' braces with the parameters passed to the function call.  These extra braces "escape" 
      ' the braces that must enclose a JavaScript function's logic, essentially telling 
      ' String.Format to not replace the contents of these particular braces.
      Dim scriptBlock As String = "" & ControlChars.CrLf & "                " & ControlChars.CrLf & "                function onLoadFunction(){{" & ControlChars.CrLf & "                  Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(AsyncResponseHandler);" & ControlChars.CrLf & "                }}" & ControlChars.CrLf & ControlChars.CrLf & "                function AsyncResponseHandler(sender, args) {{" & ControlChars.CrLf & "                  var dataItems = args.get_dataItems();" & ControlChars.CrLf & "                  if (dataItems['{0}'] != null)" & ControlChars.CrLf & "                    ESRI.ADF.System.processCallbackResult(dataItems['{0}']);" & ControlChars.CrLf & "                }}" & ControlChars.CrLf & ControlChars.CrLf & "                Sys.Application.add_init(onLoadFunction);"

      ' Insert the client ID of the page into the script block.  
      scriptBlock = String.Format(scriptBlock, Page.ClientID)

      ' Register the script on the client.  This will make the script block available client-side
      ' and execute statements that are not function or object declarations, in this case adding
      ' onLoadFunction as a handler for the init event.
      Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), scriptKeyCustom, scriptBlock, True)
    End If
  End Sub

  #End Region

  #Region "ASP.NET WebControl Event Handlers"

  ' Fires when the Zoom To Point button is clicked
  Protected Sub Button1_Click(ByVal sender As Object, ByVal eventArgs As System.EventArgs)
    ' Get the values from the X and Y textboxes
    Dim xCenter As Double = Double.Parse(TextBoxX.Text)
    Dim yCenter As Double = Double.Parse(TextBoxY.Text)

    ' Calculate 1/8 the width of the current map extent
    Dim adfMapWidthEighth As Double = Map1.Extent.Width / 8

    ' Create an envelope with its center at the coordinates specified in the X and Y textboxes,
    ' and with a width one quarter that of the current map extent.
    Dim adfNewExtentEnvelope As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = New ESRI.ArcGIS.ADF.Web.Geometry.Envelope(xCenter - adfMapWidthEighth, yCenter - adfMapWidthEighth, xCenter + adfMapWidthEighth, yCenter + adfMapWidthEighth)

    ' Update the map extent to the new envelope
    Map1.Extent = adfNewExtentEnvelope

    ' Register the map's callback results as a data item so they are processed on the client
    ScriptManager1.RegisterDataItem(Page, Map1.CallbackResults.ToString(), False)
  End Sub

  ' Fires when an item on the Zoom To menu is clicked
  Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal menuEventArgs As System.Web.UI.WebControls.MenuEventArgs)
    ' Declare and initialize variables to store the bounds of the new map extent
    Dim minX As Double = 0
    Dim minY As Double = 0
    Dim maxX As Double = 0
    Dim maxY As Double = 0

    ' Check the passed-in location and initialize the extent parameters accordingly
    Select Case menuEventArgs.Item.Text
      Case "California"
        minX = -128.0
        minY = 31.0
        maxX = -111.0
        maxY = 43.0
      Case "New York"
        minX = -80.0
        minY = 40.5
        maxX = -73.0
        maxY = 45.5
      Case "Kansas"
        minX = -103.0
        minY = 35.0
        maxX = -93.0
        maxY = 42.0
      Case Else
    End Select
    ' Create a Web ADF envelope with the new extent parameters
    Dim adfNewExtentEnvelope As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = New ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minX, minY, maxX, maxY)

    ' Update the map control's extent with the newly created envelope
    Map1.Extent = adfNewExtentEnvelope

    ' Register the map's callback results as a dataItem so that they are processed on the client
    ScriptManager1.RegisterDataItem(Page, Map1.CallbackResults.ToString(), False)
  End Sub

  #End Region

  #Region "Web ADF Control Event Handlers"

  ' Fires whenever the map's extent changes
  Protected Sub Map1_ExtentChanged(ByVal sender As Object, ByVal extentEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ExtentEventArgs)
    ' New extent is the explicit envelope the Map extent will be set to.  Note that the aspect ratio of the map 
    ' extent has been adjusted to account for pixel image size / extent size discrepancies. 
    Dim adfEnvelope As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = extentEventArgs.NewExtent

    ' Update extent boundary label values.  Note that we have to update the values on both the server (via
    ' Label.Text) and the client (via CreateSetInnerContent callback results).  The update on the server
    ' makes it so that the value persists across postbacks that don't fire the ExtentChanged event.  The
    ' update on the client makes it so that the change is actually shown on the client.

    ' Set label text on the server
    LabelN.Text = adfEnvelope.YMax.ToString("N")
    LabelE.Text = adfEnvelope.XMax.ToString("N")
    LabelS.Text = adfEnvelope.YMin.ToString("N")
    LabelW.Text = adfEnvelope.XMin.ToString("N")

    ' Update label text on the client via the map control's callback results.  Since the event was fired by the
    ' map control, the map's callback results - including any we choose to add - are processed on the client without
    ' any further action (such as registering a script block or data item).
    Dim updateLabelCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent(LabelN, adfEnvelope.YMax.ToString("N"))
    Map1.CallbackResults.Add(updateLabelCallbackResult)
    updateLabelCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent(LabelE, adfEnvelope.XMax.ToString("N"))
    Map1.CallbackResults.Add(updateLabelCallbackResult)
    updateLabelCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent(LabelS, adfEnvelope.YMin.ToString("N"))
    Map1.CallbackResults.Add(updateLabelCallbackResult)
    updateLabelCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent(LabelW, adfEnvelope.XMin.ToString("N"))
    Map1.CallbackResults.Add(updateLabelCallbackResult)
  End Sub

  #End Region
End Class