Requirement: create a class library testcontrols, which contains several ASP. NET Server controls, some of which will call the client's JavaScript Functions and put these functions into testscript. js.
It is actually a simple requirement, but I did find a lot on the Internet before I got it done.ArticleYou have raised such a problem and a solution, but in the end, you can handle it with luck. Share your experience and hope you will not make any detour.
The following example shows how to simplify the above requirements. The class library still needs to be created. There is only one server control, testcontrol, which has no interface, just register the Javascript script to the webpage. In order to quickly see the effect, the script file does not contain a function, just put an alert statement, as long as the registration is successful, when the page is opened, a prompt box is displayed, indicating that the debugging is successful.
First, you must create a class library project named testcontrols.
Delete the default class1.cs file.
Create a new class testcontrol, inherited from system. Web. UI. webcontrols. webcontrol.
Create a Javascript script testscript and change the value of the "generate operation" attribute of the script from "content" to "embedded resource ".
Create a new web project for testing. You need to make some settings in Web. config.
Testscript. js
A Lert ( " Script is registered. " );
Testcontrol. CS
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web. UI. webcontrols;
Using System. Web;
Using System. Security. permissions;
Namespace Testcontrols
{
[Aspnethostingpermission (securityaction. Demand, Level = Aspnethostingpermissionlevel. Minimal)]
Public Class Testcontrol: webcontrol
{
Protected Override Void Oninit (eventargs E)
{
Base. Oninit (E );
This. Page. clientscript. registerclientscriptresource (This. GetType (),"Testcontrols. testscript. js");
}
}
}
Assemblyinfo. CS (Add the followingCode)
Using System. Web. UI;
[Assembly: webresource ( " Testcontrols. testscript. js " , " Application/X-Javascript " )]
Web. config
< Pages >
< Controls >
< Add Tagprefix = "Tc" Assembly = "Testcontrols" Namespace = "Testcontrols" />
</ Controls >
</ Pages >
Default. aspx
< TC: testcontrol Runat = "Server" ID = "Testcontrol1" />
Basically, the above steps have been completed, and the results are very definite.
Summary:
1. the Javascript file must be changed to "embedded resources ".
2. To add a description of webresource to the class library embedded in a Javascript file, you can either put it in a separate file or assemblyinfo. CS.
3. For a javascript class, a security description must be added.
4. script file name, which is strictly in the format of the namespace plus the file name, is almost impossible.
5. Call controls