Monday, November 28, 2011

Using External Javascript, CSS or Image File in a WebPart


// Referring External Javascript 
ClientScriptManager cs = Page.ClientScript;
// Include the required javascript file.
if (!cs.IsClientScriptIncludeRegistered("jsfile"))
cs.RegisterClientScriptInclude(this.GetType(), "jsfile", "/_wpresources/MyWP/1.0.0.0_9f4da00116c38ec5/jsfile.js");

Test : 
Testbutton= new Button();
Testbutton.Text = "Click me";
Testbutton.OnClientClick = "jsfile_Function()"; // specify function name here
this.Controls.Add(Testbutton); 

// Refering External CSS 
Microsoft.SharePoint.WebControls.CssLink cssLink = new Microsoft.SharePoint.WebControls.CssLink();
cssLink.DefaultUrl = "/_wpresources/MyWP/1.0.0.0_9f4da00116c38ec5/styles.css";
this.Page.Header.Controls.Add(cssLink);

// Using External Image 
imagePath = "/_wpresources/MyWP/1.0.0.0_9f4da00116c38ec5/Image.jpg";
img.ImageUrl = imagePath;
img.ID = "image1";

this.Controls.Add(mybutton); 
this.Controls.Add(img); 

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...