How to use ajax in web forms if application xhtmlConformance mode is set to Legacy in web config

If xhtmlConformance mode is set to legacy in web config then, to use ajax or ajax enabled controls in application really becomes a pain.

There are two ways in which you can use ajax in your application:

1) either change the xhtmlConformance mode to transitional from legacy. But this cannot be changed all times, especially when you are supporting an age-old application because changing this mode might result in throwing up errors in various places. Hence probably second option is better.

2) If you are creating a new page or just want to use ajax enabled controls in only some specific pages then this approach is favourable.
Create a new class called as Base Page & inherit this from System.Web.UI.Page, inside this create a property XhtmlConformanceMode and implement getter & setter
for same.

 /// <summary>
 /// This class can be inherited in other pages where Ajax needs to be enabled.
 /// </summary>
 public class BasePage : Page
 {
 static PropertyInfo _getMode;
 static FieldInfo _setMode;
 static FieldInfo _setModeSet;
 
 static BasePage()
 {
 Type t = typeof(Page);
 _getMode = t.GetProperty("XhtmlConformanceMode", BindingFlags.Instance | BindingFlags.NonPublic);
 _setMode = t.GetField("_xhtmlConformanceMode", BindingFlags.Instance | BindingFlags.NonPublic);
 _setModeSet = t.GetField("_xhtmlConformanceModeSet", BindingFlags.Instance | BindingFlags.NonPublic);
 }
 
 public XhtmlConformanceMode XhtmlConformanceMode
 {
 get
 {
 return (XhtmlConformanceMode)_getMode.GetValue(this, null);
 }
 set
 {
 _setMode.SetValue(this, value);
 _setModeSet.SetValue(this, true);
 }
 }
 }

Now inherit this page in any of the pages where you want to use enable ajax usage.
Set the property XhtmlConformanceMode from this page either in design mode or code behind, example shown below.

//Code Behind
 
public partial class BillingFileTrack : BasePage
 
//Designer
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BillingFileTrack.aspx.cs" Inherits="medAppz2.PMS.Billing.BillingFileTrack" XhtmlConformanceMode="Transitional" %>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s