How to embed Javascript script files in the ASP. NET Server Control Library

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.