in

Jason Clifford

Migrating from ASP.NET 1.1 to 2.0 Code Changes - ClientScripts & KeyFile

In the process of converting from an ASP.NET 1.1 solution to 2.0 I have found a few more common code changes as I have been clearing out all the compiler warnings.  This should be my last post on this subject as I know finally have 0 errors and 0 warnings.

The 2 last changes that I found had to deal with registering client scripts and with AssemblyKeyFile.

Registering Client Scripts

In ASP.NET 1.1 all the clients script functions are located in the Page object like this: Page.RegisterStartupScript().  In 2.0 they are all grouped into a class that is under the page object called ClientScript so it now looks like this: Page.ClientScript.RegisterStartupScript().  This is not the only change though as now several of the register functions take a new parameter.  In 1.1 they took 2 parameters the first one was a key for the script that was being registered and the second being the script itself.  Now a 3rd parameter has been added which ends up being part of the key to further make the script unique and this is a type.  Perviously if 2 controls registered a script with the same key only one would be registered and the 2nd script, even though it is different, would not be added to the page.  With the addition of type both those controls can register the same key but with different types (the type of the control) and register both scripts.  If you want the type to be global, so it works like it used to, you can use the type of the Page class.

In order to fix these I just did a find and replace all for each of the function calls that were used in the solution.  I will show how I replaced a few of them but most of the Register functions can be fixed in a similar way.  In the case of inserting a type it is safe to use typeof(System.Web.UI.Page) since in 1.1 all the register keys needed to be unique anyway. 

Replace All ".RegisterStartupScript(" with ".ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page),"

Replace All ".RegisterClientScriptBlock(" with ".ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page),"

Replace All ".RegisterHiddenField" with ".ClientScript.RegisterHiddenField"

Replace All ".IsClientScriptBlockRegistered(" with ".ClientScript.IsClientScriptBlockRegistered(typeof(System.Web.UI.Page),"

Note: You may need to tweak these some based on how they are used but these should work given that you are upgrading from 1.1 to 2.0.

Assembly Key File

This was one of the warnings in the list: Use command line option '/keyfile' or appropriate project settings instead of 'AssemblyKeyFile'

After some digging I found that using [assembly: AssemblyKeyFile("..\\..\\StrongNameKey.snk")] is now obsolete and this needs to be set in the properties of the project under Signing.  The reason for this is that adding the strongly named key is now done as a compiler argument since when it is compiled with the assemply property the original file path can be determined. 

To set the strong name key for the project right click the project, select properties then select the signing tab.  Once here check Sign the Assembly and select the path.  Now just remove the AssemblyKeyFile property from the assembly.cs file.

Only published comments... Jan 30 2008, 12:46 PM by jclifford

Leave a Comment

(required)  
(optional)
(required)  
Add
Inetium, LLC. Disclaimer