Dll控件里含图片,JS,CSS

6 篇文章 0 订阅

今天在网上查资料,不小心被我找到了dll控件里添加外部CSS的方法,上次制作控件的疑惑终于解决了一部分,嘿嘿。

虽然文章是英文的,本人英文也不好,但是示例还是简单明了的,一看就懂了

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.

  将js文档编译成动态链接库(dll)文档

 

1.向项目中添加Jscript文档

//script_1.js-----
function doClick1()
{
    alert("OK1_wufeng");
}
//script_2.js-----
function doClick2()
{
    alert("OK2");
}

2.解决方案资源管理器中,右键查看script_1.js和script_2.js的属性,把高级中的“生成操作”属性配置成“嵌入的资源”。

3.向AssemblyInfo.cs文档中添加如下行:(注意域名wf.ClientScriptResourceLabel)

[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_1.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_2.js", "application/x-javascript")]

4.向项目中添加一个类, 实例:

using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;

namespace wf.ClientScriptResourceLabel
{
    public class ClientScriptResourceLabel : System.Web.UI.WebControls.WebControl
    {
        //调用脚本资源
        protected override void OnPreRender(EventArgs e)
        {
            if (this.Page != null)
            {
                this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_1.js");
                this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_2.js");
            }
            base.OnPreRender(e);
        }

        /// <summary>
        /// 呈现控件的方法RenderContents
        /// </summary>
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.AddAttribute("id", "1");
            output.AddAttribute("type", "checkbox");
            output.AddAttribute("value", "测试1");
            output.AddAttribute("onclick", "javascript:doClick1();");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();

            output.AddAttribute("id", "2");
            output.AddAttribute("type", "checkbox");
            output.AddAttribute("value", "测试2");
            output.AddAttribute("onclick", "javascript:doClick2();");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();

            base.RenderContents(output);
        }
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值