[转]Dll控件里含图片,JS,CSS
2007-09-03 14:39
260 查看
今天在网上查资料,不小心被我找到了dll控件里添加外部CSS的方法,上次制作控件的疑惑终于解决了一部分,嘿嘿。
虽然文章是英文的,本人英文也不好,但是示例还是简单明了的,一看就懂了
Listing 1: AssemblyInfo.cs entries
Namespace Note
The project’s default namespace (defined in the Application tab of the project's Properties page) will be added as a prefix to the filename of embedded resources. In this case, I’ve set the default namespace to an empty string. Otherwise, the tag’s first parameter would need to be DefaultNamespace.Filename.Extension instead of simply Filename.Extension. (This was the biggest pitfall I encountered because it wasn’t obvious that the namespace would be added as a prefix, and so I was referencing the resources by their short names when I should have been using the long format.)
Accessing the Embedded Resource
To add the embedded resource to our ASP.NET page, we will be calling the ClientScriptManager’s GetWebResourceUrl method. Its first parameter is the Type of the control’s class (which will eventually provide .NET with an assembly reference where the embedded resource is contained) and its second parameter is the name of the resource as specified in the AssemblyInfo.cs file. For example, to load an image in an Image control, use the code in Listing 2. To add a stylesheet to the Page header area, use the code in Listing 3. To render a JavaScript include tag, use the code in Listing 4.
Listing 2: Setting an image control’s source
Listing 3: Adding a stylesheet to the page header
Listing 4: Rendering a javascript include
ClientScriptManager Quirks
One of the things I encountered when working with this technique was that most examples of server controls do their “thing” during the Render event. So, I attempted to be a good corporate citizen and have my controls behave the same way and I found that I could not use RegisterClientScriptBlock at this point in the page life cycle. RegisterStartupScript seemed to work fine in this scenario and IE 6.0 apparently tolerates rendering styles at this point and applying them to the page’s rendered contents, but I wasn’t happy with that. So, I discovered that I had to make all calls to RegisterClientScriptBlock on or before the OnPreRender event for them to be added to the correct part of the page.
Conclusion
We’ve covered how you can embed resources in your server control projects and how to reference them in a web environment. This should simplify your life from a deployment perspective, and should make your server controls tighter now that they can leverage static images and files, and do not require any installation other than deployment of the compiled assembly.
希望对其他人也有所帮助。
虽然文章是英文的,本人英文也不好,但是示例还是简单明了的,一看就懂了
Listing 1: AssemblyInfo.cs entries
[assembly: System.Web.UI.WebResource("myImage.gif", "img/gif")] [assembly: System.Web.UI.WebResource("myStylesheet.css", "text/css")] [assembly: System.Web.UI.WebResource("myJavascript.js", "text/js")]
Namespace Note
The project’s default namespace (defined in the Application tab of the project's Properties page) will be added as a prefix to the filename of embedded resources. In this case, I’ve set the default namespace to an empty string. Otherwise, the tag’s first parameter would need to be DefaultNamespace.Filename.Extension instead of simply Filename.Extension. (This was the biggest pitfall I encountered because it wasn’t obvious that the namespace would be added as a prefix, and so I was referencing the resources by their short names when I should have been using the long format.)
Accessing the Embedded Resource
To add the embedded resource to our ASP.NET page, we will be calling the ClientScriptManager’s GetWebResourceUrl method. Its first parameter is the Type of the control’s class (which will eventually provide .NET with an assembly reference where the embedded resource is contained) and its second parameter is the name of the resource as specified in the AssemblyInfo.cs file. For example, to load an image in an Image control, use the code in Listing 2. To add a stylesheet to the Page header area, use the code in Listing 3. To render a JavaScript include tag, use the code in Listing 4.
Listing 2: Setting an image control’s source
Image theImage = new Image(); theImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myImage.gif");
Listing 3: Adding a stylesheet to the page header
string includeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />"; string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myStylesheet _Links.css"); LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation)); ((HtmlControls.HtmlHead) Page.Header).Controls.Add(include);
Listing 4: Rendering a javascript include
string scriptLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "MSDWUC_WindowStatus.js"); Page.ClientScript.RegisterClientScriptInclude("MSDWUC_WindowStatus.js", scriptLocation);
ClientScriptManager Quirks
One of the things I encountered when working with this technique was that most examples of server controls do their “thing” during the Render event. So, I attempted to be a good corporate citizen and have my controls behave the same way and I found that I could not use RegisterClientScriptBlock at this point in the page life cycle. RegisterStartupScript seemed to work fine in this scenario and IE 6.0 apparently tolerates rendering styles at this point and applying them to the page’s rendered contents, but I wasn’t happy with that. So, I discovered that I had to make all calls to RegisterClientScriptBlock on or before the OnPreRender event for them to be added to the correct part of the page.
Conclusion
We’ve covered how you can embed resources in your server control projects and how to reference them in a web environment. This should simplify your life from a deployment perspective, and should make your server controls tighter now that they can leverage static images and files, and do not require any installation other than deployment of the compiled assembly.
希望对其他人也有所帮助。
相关文章推荐
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- [转]C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- js+css图片自动等比例缩小且垂直居中[v20071208]
- javascript预加载图片、css、js的方法示例介绍
- Spring MVC程序中得到静态资源文件css,js,图片
- JS+CSS控制图片向上无缝滚动代码
- JS+CSS实现自动改变切换方向图片幻灯切换效果的方法
- js动态改变控件的css以及js检查全半角
- 雅虎军规前端35条优化(css部分 js部分 图片 cookie 移动端 服务器)
- JS+CSS实现4张图片滚动切换代码
- 如何封装JS和CSS文件封装为服务器端控件---ASP.NET 2.0
- CI 引入JS,CSS,图片的路径问题
- 通过struts.xml配置映射后的jsp引入外部css和js的路径问题及css中图片url路径问题
- js\css 让图片垂直居中
- js+css图片自动等比例缩小且垂直居中
- CSS+JS控制图片展示效果
- IIS7 无法显示 htm js 图片 css的问题
- 这是一个简单的图片时间器;使用html+css+js,合适新手练习