[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:
- 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
- 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: