using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using PhotoManage;

namespace PhotoControl
{
    [DefaultProperty("PhotoID")]
    [ToolboxData("<{0}:PhotoImage runat=server></{0}:PhotoImage>")]
    public class PhotoImage : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        private string _PhotoID;
        private string _NavigateUrl;
        private Targer _Target;
        private string _SqlConn;
        private string _ToolTip;
        private string _NOPhotoUrl;

        /// <summary>
        /// 数据库链接字符串
        /// </summary>
        [Description("数据库链接字符串"), Category("Appearance")]
        public string SqlConn
        {
            get
            {
                return _SqlConn;
            }

            set
            {
                _SqlConn = value;
            }
        }


        /// <summary>
        /// 要显示的照片ID
        /// </summary>
        [Description("要显示的照片ID"), Category("Appearance")]
        public string PhotoID
        {
            get
            {
                return _PhotoID;
            }

            set
            {
                _PhotoID = value;
            }
        }

 

        /// <summary>
        /// 链接地址
        /// </summary>
        [Description("链接地址"), Category("Appearance")]
        public string NavigateUrl
        {
            get
            {
                return ((_NavigateUrl == null) ? String.Empty : _NavigateUrl);
            }

            set
            {
                _NavigateUrl = value;
            }
        }

        /// <summary>
        /// 链接打开方式
        /// </summary>
        [Description("链接打开方式"), Category("Appearance")]
        //[DefaultValue("_blank")]
        public Targer Target
        {
            get
            {
                return _Target;
            }

            set
            {
                _Target = value;
            }
        }

        /// <summary>
        /// 将鼠标移到控件上时显示的工具提示。
        /// </summary>
        [Description("将鼠标移到控件上时显示的工具提示。"), Category("Appearance")]
        public string ToolTip
        {
            get
            {
                return ((_ToolTip == null) ? String.Empty : _ToolTip);
            }

            set
            {
                _ToolTip = value;
            }
        }

        /// <summary>
        /// 无照片时使用的替换图片
        /// </summary>
        [Description("无照片时使用的替换图片"), Category("Appearance")]
        public string NOPhotoUrl
        {
            get
            {
                return ((_NOPhotoUrl == null) ? String.Empty : _NOPhotoUrl);
            }

            set
            {
                _NOPhotoUrl = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            string outStr = "";

            if (this.PhotoID != null && this.PhotoID != "" && this.SqlConn != null && this.SqlConn != "")
            {

                PhotoDetail pDetail = new PhotoManage.PhotoDB(this.SqlConn).getPhotoDetail(this.PhotoID);
                if (pDetail != null)
                {
                    string tar = "";
                    switch (this.Target)
                    {
                        case Targer._blank: tar = "_blank"; break;
                        case Targer._parent: tar = "_parent"; break;
                        case Targer._search: tar = "_search"; break;
                        case Targer._self: tar = "_self"; break;
                        case Targer._top: tar = "_top"; break;
                    }
                    if (this.NavigateUrl == null || this.NavigateUrl == string.Empty)
                        this.NavigateUrl = pDetail.PhotoPath;                   

                    if (this.Height.IsEmpty != true && this.Width.IsEmpty != true)
                        outStr = "<a href=\"" + this.NavigateUrl + "\" target=\"" + tar + "\"><img border=\"0\" height=\"" + this.Height + "\" width=\"" + this.Width + "\" src=\"" + pDetail.SmallPhotoPath + "\" Alt=\"" + this.ToolTip + "\"/></a>";
                    else if (this.Height.IsEmpty == true && this.Width.IsEmpty == false)
                        outStr = "<a href=\"" + this.NavigateUrl + "\" target=\"" + tar + "\"><img border=\"0\" width=\"" + this.Width + "\" src=\"" + pDetail.SmallPhotoPath + "\" Alt=\"" + this.ToolTip + "\"/></a>";
                    else if (this.Height.IsEmpty == false && this.Width.IsEmpty == true)
                        outStr = "<a href=\"" + this.NavigateUrl + "\" target=\"" + tar + "\"><img border=\"0\" height=\"" + this.Height + "\" src=\"" + pDetail.SmallPhotoPath + "\" Alt=\"" + this.ToolTip + "\"/></a>";
                    else
                        outStr = "<a href=\"" + this.NavigateUrl + "\" target=\"" + tar + "\"><img border=\"0\" src=\"" + pDetail.SmallPhotoPath + "\" Alt=\"" + this.ToolTip + "\"/></a>";
                    output.Write(outStr);
                    return;
                }
                else
                {
                    outStr = "<img border=\"0\" src=\"" + this.NOPhotoUrl + "\" alt=\"暂无照片\"";
                    output.Write(outStr);
                    return;
                }

            }
            else
            {
                outStr = "<img border=\"0\" alt=\"未提供属性SqlConn或者属性PhotoID\"";
                output.Write(outStr);
                return;
            }
        }
    }

   public enum Targer
    {
        _blank = 0,
        _parent,
        _search,
        _self,
        _top
    }
}

 

--------------------------------------------------------------------------------

调用部分:

        PhotoImage1.PhotoID = "200DBF2A-5DE1-411C-8525-243B4D6D7177";
        PhotoImage1.SqlConn = sqlURL;
        PhotoImage1.NavigateUrl = http://www.qq.com";
        PhotoImage1.ToolTip = "腾讯";
        PhotoImage1.Target = PhotoControl.Targer._blank;
        PhotoImage1.NOPhotoUrl = "Alite.gif";
        PhotoImage1.Width = 50;
        PhotoImage1.Height = 60;