玩转用户控件-统计控件2

在1的基础上修改成:
动态的背景设置、动态的显示条设置
以及显示条的Class样式支持。

前台代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PointProgess.ascx.cs"
    Inherits="UserControl_PointProgess" %>
<table id="htmlTblProgress"  runat="server" border="0" cellpadding="1" cellspacing="0">
    <tr>
        <td>
            <div id="divProgress" runat="server">
                &nbsp;
            </div>
        </td>
    </tr>
</table>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_PointProgess : System.Web.UI.UserControl
{

    int _MaxValue = 10;
    int _CurrentValue = 1;
    int _ProgressWidth = 100;
    int _ProgressHeight = 10;

    /// <summary>
    /// 默认样式
    /// </summary>
    Color _BarBackGroundColor = Color.Silver;
    Color _ShowColor = Color.Black;
    string _ShowBarCss = "";
   

    #region 属性

    /// <summary>
    /// 设置最大值
    /// </summary>
    public int MaxValue
    {
        get
        {
            return this._MaxValue;
        }
        set
        {
            this._MaxValue = value;
        }
    }

    /// <summary>
    /// 设置当前值
    /// </summary>
    public int CurrentValue
    {
        get
        {
            return this._CurrentValue;
        }
        set
        {
            this._CurrentValue = value;
        }
    }

    /// <summary>
    /// 设置显示条长度
    /// </summary>
    public int ProgressWidth
    {
        get
        {
            return this._ProgressWidth;
        }
        set
        {
            this._ProgressWidth = value;
        }
    }

    /// <summary>
    /// 设置显示条高度
    /// </summary>
    public int ProgressHeight
    {
        get
        {
            return this._ProgressHeight;
        }
        set
        {
            this._ProgressHeight = value;
        }
    }

    #endregion

    #region 第2次附加属性

    /// <summary>
    /// 背景颜色
    /// </summary>
    public Color BarBackGroundColor
    {
        get
        {
            return this._BarBackGroundColor;
        }
        set
        {
            this._BarBackGroundColor = value;
        }
    }

    /// <summary>
    /// 进度条显示颜色
    /// </summary>
    public Color ShowColor
    {
        get
        {
            return this._ShowColor;
        }
        set
        {
            this._ShowColor = value;
        }
    }

    /// <summary>
    /// 设置进度条的样式
    /// </summary>
    public string ShowBarCss
    {
        get
        {
            return this._ShowBarCss;
        }
        set
        {
            this._ShowBarCss = value;
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        ShowProgress();
    }

    /// <summary>
    /// 显示显示条
    /// </summary>
    protected void ShowProgress()
    {
        //背景处理
        htmlTblProgress.Height = this._ProgressHeight+"px";
        htmlTblProgress.Width = this._ProgressWidth+"px";
        htmlTblProgress.BgColor = "#"+this._BarBackGroundColor.R.ToString("X2")+this._BarBackGroundColor.G.ToString("X2")+this._BarBackGroundColor.B.ToString("X2");
      

        int divWidth = (this._CurrentValue * this._ProgressWidth) / this._MaxValue;

        if (divWidth > this._ProgressWidth)
        {
            divWidth = this._ProgressWidth;
        }
        if (divWidth < 0)
        {
            divWidth = 0;
        }

        //显示条样式处理
        divProgress.Style.Add(HtmlTextWriterStyle.Width, divWidth+"px");
        divProgress.Style.Add(HtmlTextWriterStyle.Height, _ProgressHeight + "px");
        if (this._ShowBarCss == "")
        {
            divProgress.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#" + this.ShowColor.R.ToString("X2") + this.ShowColor.G.ToString("X2") + this.ShowColor.B.ToString("X2"));
        }
        else
        {
            divProgress.Attributes.Add("class", this._ShowBarCss);
        }

    }

}



页面调用:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register TagPrefix="LandySkyCtrl" TagName="ProgressBar" Src="UserCtrl/PointProgess.ascx" %>
<!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">
    <title></title>
    <style type="text/css">
    .BarBackStyle
    {
     background-image:url(images/backBar.jpg);
     background-repeat:repeat-x;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <LandySkyCtrl:ProgressBar ID="ProgressBar1" runat="server" ShowBarCss="BarBackStyle" MaxValue="100" CurrentValue="50"
            ProgressHeight="20" ProgressWidth="200" />
        <br />
        <br />
        <LandySkyCtrl:ProgressBar ID="ProgressBar2" runat="server" MaxValue="100" CurrentValue="40"
            ProgressHeight="20" ProgressWidth="200" BarBackGroundColor="#8BDBF6" ShowColor="Red" />
    </div>
    </form>
</body>
</html>

有好的点子都请大伙提出。

posted @ 2009-09-28 17:30  幻蓝  阅读(927)  评论(1)    收藏  举报