在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
代码: using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DemoWebControl { /// <summary> /// 自己的Asp.Net服务器组件 Image:输出内嵌的在程序集中的 图片资源 /// </summary> public class DemoImage : WebControl { protected bool IsDesignMode { get { return DesignMode || HttpContext.Current == null || Page == null; } } public DemoEnumImage DemoEnumImage { get; set; } //通过用户在 属性中的设置,将内嵌的图片 作为 IMG标签 输出到 页面 protected override void Render(HtmlTextWriter output) { string imageUrl = string.Empty; if (Page!=null) { if(DemoEnumImage==DemoEnumImage.Image01) imageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), @"DemoWebControl.Resources.DemoImage01.jpg"); else if (DemoEnumImage == DemoEnumImage.Image02) imageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), @"DemoWebControl.Resources.DemoImage02.jpg"); else if (DemoEnumImage == DemoEnumImage.Image03) imageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), @"DemoWebControl.Resources.DemoImage03.jpg"); } output.Write("<image id=\"" + ClientID + "\" name=\"" + ClientID + "\" class=\"" + CssClass + "\" src=\"" + imageUrl + "\" />"); } } [Serializable] public enum DemoEnumImage { Image01, Image02, Image03, } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoImagePage.aspx.cs" Inherits="AspNetDemo.DemoImagePage" %> <%@ Register assembly="DemoWebControl" namespace="DemoWebControl" tagprefix="Demo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <style type="text/css"> .Demo{ border: 1px solid RED; } </style> </head> <body> <form id="form1" runat="server"> <div> <Demo:DemoImage ID="DemoImage1" DemoEnumImage="Image03" CssClass="Demo" runat="server" /> </div> </form> </body> </html>
嵌入资源:
运行截图:
相关技术点:
相关系列文章链接:
|
请发表评论