10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8<br />

Listing 8-2: RegisterTypeFor<strong>Ajax</strong> Definition in /<strong>Ajax</strong>/Utility.cs<br />

01. public static void RegisterTypeFor<strong>Ajax</strong>(Type type)<br />

02. {<br />

03. System.Web.UI.Page page =<br />

(System.Web.UI.Page)System.Web.HttpContext.Current.Handler;<br />

04. RegisterTypeFor<strong>Ajax</strong>(type, page);<br />

05. }<br />

06.<br />

07. public static void RegisterTypeFor<strong>Ajax</strong>(Type type, System.Web.UI.Page page)<br />

08. {<br />

09. RegisterCommon<strong>Ajax</strong>(page);<br />

10. string path = type.FullName + “,” + type.Assembly.FullName.Substring(0,<br />

type.Assembly.FullName.IndexOf(“,”));<br />

11. if(Utility.Settings.UseAssemblyQualifiedName) path =<br />

type.AssemblyQualifiedName;<br />

12.<br />

13. if(Utility.Settings != null &&<br />

Utility.Settings.UrlNamespaceMappings.ContainsValue(path))<br />

14. {<br />

15. foreach(string key in Utility.Settings.UrlNamespaceMappings.Keys)<br />

16. {<br />

17. if(Utility.Settings.UrlNamespaceMappings[key].ToString() == path)<br />

18. {<br />

19. path = key;<br />

20. break;<br />

21. }<br />

22. }<br />

23. }<br />

24.<br />

25. RegisterClientScriptBlock(page, “<strong>Ajax</strong>Type.” + type.FullName,<br />

“”);<br />

26. }<br />

The first thing that happens in the RegisterTypeFor<strong>Ajax</strong>() method is that the Utility.Register<br />

Common<strong>Ajax</strong>() method is called on line number 09. The RegisterCommon<strong>Ajax</strong>() method is responsible<br />

for adding script references to the prototype, core, and converter files. This method makes sure to<br />

register these files only once per page, even if you’re registering multiple types. You’ll see how this is<br />

done shortly with the inspection of the Settings object.<br />

At this point, you know that the prototype, core, and converter scripts have been registered, and<br />

you’re ready to move on and create a reference to your own type. Line 10 creates a string that consists of the<br />

type’s fully qualified name and the assembly that the type is in. This results in the /Chapter7_OnLoading,<br />

App_Web_rjv9qbqt found in line 2 in Listing 8-1. This value is checked against a HashTable named<br />

Utility.Settings.UrlNamespaceMappings. If the value is found, line 20 calls break, and the method<br />

replaces the type name with the mapped name. You’ll take look at this UrlNameSpaceMappings shortly.<br />

If the value is not found, then you know to simply use the type name as the script source.<br />

184

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!