Webpart references. CSS files and. js files

Source: Internet
Author: User

[Original] webpart references. CSS files and. js files

 

Yesterday, I suddenly had an idea: Could webpart reference CSS files and JS files, and then I could hardly see such information on the Internet, but it was not solved; I just got a book named "Liang Jian" online yesterday. It's okay to read it in the morning. Oh, it's printed into my eyes. I'll write it out and share it with you, please point out something bad;

In the followingCodeIs very simple, here I do not want to show my code, but to talk about a method:

    1. First, define two files, one is a. CSS file and the other is. JS files, and then place them in c: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ layouts \ create a folder codeart under this directory, put both files here;

      The code for these two files is as follows:

      . CSS file

      I believe everyone understands this;

      . JS File

    2. The next step is how the webpart code registers the two files;

The Code is as follows:

UsingSystem;

UsingSystem. runtime. interopservices;

UsingSystem. Web. UI;

UsingSystem. Web. UI. webcontrols;

UsingSystem. Web. UI. webcontrols. webparts;

UsingSystem. xml. serialization;

 

UsingMicrosoft. SharePoint;

UsingMicrosoft. Sharepoint. webcontrols;

UsingMicrosoft. Sharepoint. webpartpages;

UsingSystem. Web. UI. htmlcontrols;

 

NamespaceWebpart_css

{

[Guid("81236f7a-a38a-4744-a532-67ff6faca56b")]

Public Class Webpart_css: System. Web. UI. webcontrols. webparts.Webpart

{

PublicWebpart_css ()

{

}

// Declare the directory where resource files such as pages are stored

Public Const StringResource_path ="~ /_ Layouts/codeart /";

 

/// <Summary>

///Register a CSS file

/// </Summary>

/// <Param name = "FILENAME">CSS file name</Param>

Public VoidRegistercommoncss (StringCssfilename)

{

StringId = cssfilename. tolower (). Replace (".","_");

 

// Determine whether the CSS file has been added

Foreach(ControlCTLIn This. Page. header. Controls)

{

If(CTL. ID = ID)

{

Return;

}

}

 

// Create a CSS file Link Control

HtmllinkLink1 =New Htmllink();

Link1.id = ID;

Link1.attributes ["Type"] ="Text/CSS";

Link1.attributes ["Rel"] ="Stylesheet";

Link1.attributes ["Href"] =Base. Resolveurl (resource_path) + cssfilename;

This. Page. header. Controls. Add (link1 );

}

/// <Summary>

///Registration script

/// </Summary>

/// <Param name = "jsfilename">Script File Name</Param>

Public VoidRegistercommonjs (StringJsfilename)

{

// Script path

StringJspath =Base. Resolveurl (resource_path) + jsfilename;

// Register the script

Page. clientscript. registerclientscriptinclude (Typeof(Webpart_css), Jsfilename. tolower (), jspath );

}

Protected Override VoidOnprerender (EventargsE)

{

// Register the CSS file

Registercommoncss ("Test.css");

// Register the JS File

Registercommonjs ("Test. js");

Base. Onprerender (E );

}

 

Public LabelLable =Null;

Protected Override VoidCreatechildcontrols ()

{

Lable =New Label();

Lable. Text ="I'm Zhang Jinshan";

Lable. cssclass ="Fontcss";

This. Controls. Add (lable );

 

 

StringStrinnerhtml ="<Input type = 'button 'id = 'btnsearch' value = 'search' onclick = 'javascript: testjs () '/>";

HtmlgenericcontrolHtml =New Htmlgenericcontrol();

Html. innerhtml = strinnerhtml;

This. Controls. Add (HTML );

}

}

}

 

In this way, you can see the following results after deployment:

Click the following button:

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.