ASP.NET AJAX Tips And Tricks

How to Reference ScriptManager from the Code Hehind

You need to add the namespace Microsoft.Web.UI to your source code and aslo add an assembly reference to Microsoft.Web.Extensions.dll in your project file.

   
Sample:
                using Microsoft.Web.UI;
                protected ScriptManager ScriptManager1;
                ...
                ScriptManager1.IsInAsyncPostback 

Handy Debug Tool for ASP.NET AJAX

Web Development Helper utility is an awesome tool. It is an Internet Explorer plugin that provides a set of useful tools to both Ajax/JavaScript developers as well as ASP.NET page and control developers. For client-side script-based development, Web Development Helper provides HTTP tracing capabilities, as well as much improved script diagnostics, and tracing facilities, as well as an immediate window. For ASP.NET developers, when developing against your site on your local development machine, this tool provides the ability to view ViewState, ASP.NET trace messages, contents of your cache etc.

The following is a list of features offered by the tool:

  1. A warning when either debug or trace have been turned on. Ability to hide trace information from the page, and view it in a separate dialog, so it does not get in the way of your page's layout.
  2. Ability to view the page's view state (in various forms: raw, parsed, and decoded) to help you optimize your state management logic.
  3. Ability to view items stored in cache, and remove them for testing purposes.
  4. Ability to shutdown the application (unloading the associated appdomain), in order to test starting it from a clean state.
  5. Ability to log all HTTP (and HTTPS) traffic between the browser and your server, and view request and response details, including those performed via XMLHttpRequest.
  6. Ability to view the live HTML DOM using a DOM Inspector that allows viewing all elements, selected elements, or elements matching ID or CSS class. For each element, you can see the markup, attributes, and style attributes.
  7. Ability to view rich error information for script errors (full call stack, script URL and line number), as well as a script immediate window to execute some script against the current document.

By the way viewing JavaScript code can be very easy with this addon: https://addons.mozilla.org/en-US/firefox/addon/2076

Response.Redirect Doesn't Work

The old tricks of ASP may not work any more with Ajax, like:

    Response.Redirect(url);
or 
    Response.Write("<script>window.open(' " + url + "','');</script>");
You may get an error says something like "The message received from the server could not be parsed". To get around this, I have a tip for you:
    protected void Button1_Click(object sender, EventArgs e)
    {
        RedirectTo(url);
    }

    private void RedirectTo(string url)
    {
        string script = "window.open('" + url + "');";
        Page page = (Page)Context.Handler;
        ScriptManager.RegisterStartupScript(page, typeof(Page), "RedirectTo", script, true);
    }

How to Use microsoftajax.js Directly

You may want to modify and use microsoftajax.js directly, which is included as web resources and downloaded through the .axd. So the solution is you can copy the script into your application's script folder and set the ScriptPath on the ScriptManager. The ScriptManager will pick up the scripts from the physical folder instead of from the assembly reseources.

Why document.write Doesn't Work in Partial Postback?

The document.write can't be used after the DOM is loaded completely. After the document's readyState is "complete" the browser treats any document.write as a new DOM and clears out the existing DOM. Thus in a partial postback, it will always fail since the DOM would already be loaded.

What Are === and !==

"===" and "!==" are identity and nonidentity operators in Javascript. Please check out the Comparison Operators (JScript 5.6) on MSDN for details.

How to Set Week Start Day in Calendar AJAX Extender

You need to use the property FirstDayOfWeek.

    <ajaxToolkit:CalendarExtender ID="myDateButtonExtender" runat="server" TargetControlID="myDateTextBox"
        Enabled="True" Format="dd/MM/yyyy" FirstDayOfWeek="Monday">
    </ajaxToolkit:CalendarExtender> 

Right now I am working on a project that uses Ajax for better user experience, more and more tips will be added. Thanks.

See also

  • SQL: Use Dynamic SQL Query Correctly
  • SQL 2005: Use DMV and CROSS APPLY to Get Cached Plans
  • SQL 2005: Discontinued or Deprecated Features in SQL Server 2005
  • SQL 2005: Default Trace Enabled Option
  • SQL 2005: Column Level Permissions
  • SQL 2005: SQLCMD Supports Parameterized Variables and Macro Features
  • SQL 2005: DTS Has Become SSIS now
  • SQL 2005: Microsoft SQL Server 2005 JDBC Driver
  • SQL 2005: Query Notifications in ADO.Net 2.0
  • SQL 2005: Overcome SQL Index Size Limit
  • SQL 2005: DDL Triggers
  • SQL 2005: Why Should Use 64 Bit Now
  • SQL 2005: How to Rebuild The Master Database
  • SQL 2005: A Little Trick to Install SQL 2005 Onto Your Dirty DEV Machine
  • SQL 2005: New Resource Database
  • SQL 2005: Alter Index Rebuild
  • SQL 2005: XQuery Sample
  • SQL 2005: How to Move Database
  • SQL: Use COALESCE to Generate a List
  • SQL: How to Debug SQL Deadlocks
  • .Net: How to Bypass Strong Name Check
  • Agile: Lean Software Development - An Agile Toolkit
  • ORM: How to Use nHibernate 1.2 to Call Stored Procedure to Return a Dataset Without a Mapping Entity
  • AJAX: ASP.NET AJAX Tips
  • .Net: Debugging Commands
  • .Net: How to Run NUnit And Debug Your Test Fixtures Directly from VS 2005
  • .Net: How to Add Domain User to Local Group
  • .Net: Lock Value Type?
  • .Net: How to Create an Instance of a Generic Type with Parameters
  • .Net: How to Get Address of a Managed Type
  • ORM: New Features of nHibernate 1.2
  • .Net: How to Get System Error Message from HRESULT in Managed Code
  • .Net: Use Windows PowerShell Now
  • WMI: Use WMI to Run Commands on Remote Machine
  • API: GetLogicalProcessorInformation to Detect CPUs
  • .Net: How to Implement Singleton Correctly
  • .Net: There is no MTS object context (Exception from HRESULT: 0x8004E004)
  • .Net: The Net Objectives Pattern Repository
  • Web: Access Denied When ASP.Net Accesses Eventlog
  • Nant: Error Loading GUID of Project
  • AJAX: Ajax in Action
  • DTC: DtcGetTransactionManager Fails
  • .Net: Run .Net 1.1 COM+ Serviced Components Under .Net 2.0 Framework
  • .Net: Debugging Managed Code Tip
  • .Net: Assembly Binding Log Viewer (Fuslogvw.exe)
  • .Net: .Net Framework Design Guidelines
  • .Net: Use Global Catalog and CheckTokenMembership to Check AD Group Membership



  • THIS POST IS PROVIDED "AS-IS" WITH NO WARRANTIES AND CONFERS NO RIGHTS. Build time: Sun 03/30/2008 . ©2007 Dalun Software. All rights reserved. Back to Article List