How To Embedded StyleSheet File with Custom Control.
2012-02-24 13:31
330 查看
How to embedded StyleSheet file with custom control? Question has been opened many time on ASP.NET forum.
So in this article, I will try to demonstrate how to achieve this task.
In General
In general, if you want to embedded any thing with custom control, you need first to make its "Build Action" property to be "Embedded Resource", and then you need to add to the custom control assemblies using the <Assembly> attribute typically above class
name, and finally you need to register in the page so it can be rendered.
I will illustrate all these things in details through the article.
Step One: make it as Embedded Resource
After you added the StyleSheet file to your Custom Control Class Library project, first thing you need to do is to make build action property for the stylesheet file as embedded resource, otherwise your page will not be able to see the stylesheet file.
To do this, from the VS do a right click on the stylesheet file, and click on Properties, and then change the "Build Action" property to be "Embedded Resource".
Step Two: Add it to the Custom Controls Assemblies
Second thing you need to do is to register the Css file to the custom control assemblies by adding the below code typically above class name
[assembly: WebResource("YourProjectName.StyleSheetFileName.css", "text/css")]
namespace YourNameSpace
{
public class MyCustomControl : CompositeControl
{
//Custom Control Rest Code
Step Three: Register Css File on the Page
Finally, we have to tell the Page that our custom control has Css file needed to be registered when the final HTML is generated for that page. So we need to write a bit of code in the OnInit method.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(),
"YourProjectName.CssFileName.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
Page.ClientScript.RegisterClientScriptBlock(this.GetType, "cssFile", css, false);
}
And that's it, you can now use the CSS StyleSheet file within your Custom Control.
I hope some one will found this very helpful.
So in this article, I will try to demonstrate how to achieve this task.
In General
In general, if you want to embedded any thing with custom control, you need first to make its "Build Action" property to be "Embedded Resource", and then you need to add to the custom control assemblies using the <Assembly> attribute typically above class
name, and finally you need to register in the page so it can be rendered.
I will illustrate all these things in details through the article.
Step One: make it as Embedded Resource
After you added the StyleSheet file to your Custom Control Class Library project, first thing you need to do is to make build action property for the stylesheet file as embedded resource, otherwise your page will not be able to see the stylesheet file.
To do this, from the VS do a right click on the stylesheet file, and click on Properties, and then change the "Build Action" property to be "Embedded Resource".
Step Two: Add it to the Custom Controls Assemblies
Second thing you need to do is to register the Css file to the custom control assemblies by adding the below code typically above class name
[assembly: WebResource("YourProjectName.StyleSheetFileName.css", "text/css")]
namespace YourNameSpace
{
public class MyCustomControl : CompositeControl
{
//Custom Control Rest Code
Step Three: Register Css File on the Page
Finally, we have to tell the Page that our custom control has Css file needed to be registered when the final HTML is generated for that page. So we need to write a bit of code in the OnInit method.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(),
"YourProjectName.CssFileName.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
Page.ClientScript.RegisterClientScriptBlock(this.GetType, "cssFile", css, false);
}
And that's it, you can now use the CSS StyleSheet file within your Custom Control.
I hope some one will found this very helpful.
相关文章推荐
- How To Embedded StyleSheet File with Custom Control.
- How to create a custom action type with a custom control (BarCheckItem), associated with it
- XPages Example: Building a Custom Control - How to build a Custom Multi-FileUpload Control that you can drop on any XPag
- XPages Example: Building a Custom Control - How to build a Custom Multi-FileUpload Control that you can drop on any XPa
- HOW TO Load a Custom Script with Dependencies
- Eclipse报错"No embedded stylesheet instruction for file"的解决方法
- linux - How do I write stderr to a file while using "tee" with a pipe?
- How To Create a Modeless CPropertySheet with Standard Buttons
- How to Hook Up a VS.NET 2005 Solution With CruiseControl.NET in a Few Minutes
- How to increase swap size with a swap file and Partition
- How to upload a file via ABAP RFC with Webdynpro ?
- How to group Option Button (Form control) with cell link.
- How to integrate custom security policy with Windows domain authentication in ASP.NET
- [How to] Make custom search with Nutch(v 1.0)?(转)
- custom style(css) to RichHtmlField Control(MOSS)
- No embedded stylesheet instruction for file:奇怪的错误
- How to use btt with blktrace to analysis the trace file
- How to give custom labels to x axis of chart control?
- How To Use the Spreadsheet Web Component with Visual Basic
- How to Make Custom Drawn Gradient Backgrounds in a Grouped UITableView with Core Graphics