Practice has proved that to achieve the final distribution of application resources such as JavaScript files, images, or resource files, a good way is to embed them directly into the compiled. NET assembly.
Note: The sample program test environment in this article is Windows XP Professional Edition + Visual Studio 2005 + ASP. net ajax framework.
1) Create an empty library
First, start Visual Studio 2005 and select "File> new project ...", Select C # As the built-in support language, select the "class library" template to create a class library, name it "MySampleControl", and click OK.
Right-click the "Reference" folder in Solution Explorer and click "add reference ...", In the "add reference" dialog box that appears, add references to namespaces such as System. Web, System. Drawing, and System. Web. Extensions to the current project.
2) create a sample JavaScript script file
Next, we will add a simple JScript file named UpdatePanelAnimation. js in the project. The complete code of this JScript file is shown below:
BorderAnimation = function(color) { this._color = color; } BorderAnimation.prototype = { animate: function(panelElement) { var s = panelElement.style; s.borderWidth = '2px'; s.borderColor = this._color; s.borderStyle = 'solid'; window.setTimeout( function() {{ s.borderWidth = 0; }}, 500); } } |
The code above defines a fairly simple JavaScript class-BorderAnimation. This class only provides a member function-animate, which can display a border with a specified color around the UpdatePanel control on the ASP. net ajax server side.
Next, in the UpdatePanelAnimation. js file's corresponding Property Window, set the generation method to "embedded resource", as shown in 1.
Figure 1. Set the script file generation mode to the embedded resource Mode
3) create a control class in the class library
Next, right-click the project and click "add> Class ...", In the "Add new project" dialog box that appears, select the "class" template and add a control class file named MyCustomControl. cs to the project.
Note: In this case, we can delete the file class1.cs automatically generated along with the previous class library project.
Then, open the file MyCustomControl. cs for further modification. The following code corresponds to the complete code of the control class we just created:
Using System;
Using System. Drawing;
Using System. Web. UI;
Using System. Web;
Using System. Globalization;
Namespace MySampleControl
{
Public class UpdatePanelAnimationWithClientResource: Control
{
Private string _ updatePanelID;
Private Color _ borderColor;
Private Boolean _ animate;
Public Color BorderColor {
Get
{Return _ borderColor ;}
Set
{_ BorderColor = value ;}
}
Public string UpdatePanelID
{
Get {return _ updatePanelID ;}
Set {_ updatePanelID = value ;}
}
Public Boolean Animate
{
Get {return _ animate ;}
Set {_ animate = value ;}
}
Protected override void OnPreRender (EventArgs e)
{
Base. OnPreRender (e );
If (Animate ){
UpdatePanel updatePanel = (UpdatePanel) FindControl (UpdatePanelID );
String script = String. Format (
CultureInfo. InvariantCulture,
@"
Sys. Application. add_load (function (sender, args ){{
Var {0} _ borderAnimation = new BorderAnimation ('{1 }');
Var panelElement = document. getElementById ('{0 }');
If (args. get_isPartialLoad ()){{
{0} _ borderAnimation. animate (panelElement );
}}
}})
",
UpdatePanel. ClientID,
ColorTranslator. ToHtml (BorderColor ));
ScriptManager. RegisterStartupScript (
This,
Typeof (UpdatePanelAnimationWithClientResource ),
ClientID,
Script,
True );
}
}
}
}
This control class provides attributes used to customize the border color of the UpdatePanel control. In addition, the above Code also registers the JavaScript code to be used on the Web page. The Code also creates a processor to load events of the Sys. Application object. In this way, when updating a local page, the animate function included in the previous script file UpdatePanelAnimation. js is called.
Next, we must add the following code lines to the AssemblyInfo. cs file of the Assembly property file.
[assembly: System.Web.UI.WebResource("MySampleControl.UpdatePanelAnimation.js", "application/x-javascript")] |
Note: The WebResource definition must include the namespace and the name of the. js file.
Finally, right-click the above class library project and select "generate" to build this class library project into a. dll Assembly file.
When the compilation is complete, you already have an Assembly named SampleControl. dll. The JavaScript code in the UpdatePanelAnimation. js file is "hidden" into the Assembly as an embedded resource. 2. Demonstrate the information of the Assembly SampleControl. dll observed by. NET Reflector compiled using Lutz Roeder.
Figure 2. Using. NET Reflector to analyze the result of Assembly SampleControl. dll
- Implement RSA encryption in ASP. Net
- Several methods for implementing page value passing through ASP. NET
- Advantages and disadvantages of ASP. NET client Status Management