asp.net自定义控件

回发星级控件

 

using System;

using System.ComponentModel;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;



namespace ControlLibrary

{

    public class PostStar : WebControl, IPostBackEventHandler, INamingContainer

    {

        private event EventHandler<StarEventArgs> _postScore;



        public event EventHandler<StarEventArgs> PostScore

        {

            add

            {

                _postScore += value;

            }

            remove

            {

                _postScore -= value;

            }

        }



        private void OnPostScore(object sender, StarEventArgs e)

        {

            if (_postScore != null)

                _postScore(sender, e);



            TableCell cell = ((Table)this.Controls[0]).Rows[0].Cells[1];

            Panel panCur = (Panel)cell.Controls[0].Controls[1];

            panCur.Style.Add("width", e.Score * 16 + "px");

            Panel panChange = (Panel)panCur.Controls[0];

            panChange.Style.Add("display", "none");

            HtmlGenericControl ul = (HtmlGenericControl)cell.Controls[0].Controls[0];

            ul.Style.Add("display", "none");



        }



        [DefaultValue(0)]

        public int Score

        {

            get

            {

                object obj = ViewState["Score"];

                return obj == null ? 0 : Convert.ToInt32(obj);

            }

            internal set

            {

                ViewState["Score"] = value;

            }

        }



        public string Comment

        {

            get

            {

                object obj = ViewState["Comment"];

                return obj == null ? string.Empty : Convert.ToString(obj);

            }

            set

            {

                ViewState["Comment"] = value;

            }

        }



        protected override void CreateChildControls()

        {

            base.CreateChildControls();



            CreateControlHierarchy();

        }



        protected virtual void CreateControlHierarchy()

        {

            Table table = new Table();

            TableRow row = new TableRow();

            table.Rows.Add(row);

            TableCell comment = new TableCell();

            CreateComment(comment);

            row.Cells.Add(comment);



            TableCell stars = new TableCell();



            CreateStars(stars);



            row.Cells.Add(stars);



            this.Controls.Add(table);

        }



        /// <summary>

        /// 向单元格中创建注释标签

        /// </summary>

        /// <param name="cell">单元格对象</param>

        private void CreateComment(TableCell cell)

        {

            cell.Text = Comment;

        }



        /// <summary>

        /// 向单元格中创建星形图案

        /// </summary>

        /// <param name="cell"></param>

        private void CreateStars(TableCell cell)

        {

            RegisterCSS();



            string starPath = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ControlLibrary.Image.stars.gif");



            Panel panBg = CreateBackPanel(starPath);



            cell.Controls.Add(panBg);



            Panel panCur = CreateCurrentPanel(starPath);

            Panel panChange = CreateChangePanel(starPath);

            HtmlGenericControl ul = CreateList();



            panBg.Controls.Add(ul);



            panBg.Controls.Add(panCur);

            panCur.Controls.Add(panChange);

        }



        private void RegisterCSS()

        {

            string css = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ControlLibrary.CSS.star.css");



            HtmlLink link = new HtmlLink();

            link.Href = css;

            link.Attributes.Add("rel", "stylesheet");

            link.Attributes.Add("type", "text/css");



            Page.Header.Controls.Add(link);

        }



        private Panel CreateBackPanel(string starPath)

        {

            Panel panBg = new Panel();



            panBg.ID = "divBg";

            panBg.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);



            panBg.CssClass = "stars";



            return panBg;

        }



        private Panel CreateCurrentPanel(string starPath)

        {

            Panel panCur = new Panel();



            panCur.ID = "divCur";

            panCur.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);

            panCur.CssClass = "current";



            return panCur;

        }



        private Panel CreateChangePanel(string starPath)

        {

            Panel panChange = new Panel();



            panChange.ID = "divChange";

            panChange.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);



            panChange.CssClass = "change";



            return panChange;

        }



        private HtmlGenericControl CreateList()

        {

            HtmlGenericControl ul = new HtmlGenericControl("ul");



            ul.ID = "ulist";



            ul.Attributes.Add("class", "ulist");



            for (int i = 0; i < 5; i++)

            {

                HtmlGenericControl li = new HtmlGenericControl("li");

                li.Attributes.Add("value", (i + 1).ToString());



                ul.Controls.Add(li);

            }



            return ul;

        }



        public void RaisePostBackEvent(string args)

        {

            if (!string.IsNullOrEmpty(args))

            {

                EnsureChildControls();



                int score = Convert.ToInt32(args);



                StarEventArgs e = new StarEventArgs();

                e.Score = score;



                OnPostScore(this, e);

            }

        }



        protected override void Render(HtmlTextWriter writer)

        {

            PrepareControlForRender();



            base.Render(writer);

        }



        private void PrepareControlForRender()

        {

            if (this.Controls.Count < 1)

                return;



            Table table = (Table)this.Controls[0];



            table.CellSpacing = 0;

            table.CellPadding = 0;



            TableCell cell = table.Rows[0].Cells[1];

            Panel panCur = (Panel)cell.Controls[0].Controls[1];

            Panel panChange = (Panel)panCur.Controls[0];

            HtmlGenericControl ul = (HtmlGenericControl)cell.Controls[0].Controls[0];



            for (int i = 0; i < ul.Controls.Count; i++)

            {

                HtmlGenericControl li = (HtmlGenericControl)ul.Controls[i];

                li.Attributes.Add("onmouseover", "document.getElementById('" + panChange.ClientID + "').style.width='" + 16 * (i + 1) + "px';");

                li.Attributes.Add("onmouseout", "document.getElementById('" + panChange.ClientID + "').style.width='0px';");

                li.Attributes.Add("onclick", Page.ClientScript.GetPostBackClientHyperlink(this, (i + 1).ToString()));

            }

        }

    }



    public class StarEventArgs : EventArgs

    {

        public int Score

        {

            get;

            set;

        }

    }

}

页面调用:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <cc:PostStar ID="star" runat="server" Comment="Windows XP" OnPostScore="star_PostScore" />

        <br />

        <cc:PostStar ID="star2" runat="server" Comment="VISTA" OnPostScore="star_PostScore" />

    </div>

    </form>

</body>

</html>

 

后台:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



public partial class PostStarTest : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {



    }



    protected void star_PostScore(object sender, ControlLibrary.StarEventArgs e)

    {

        Response.Write(e.Score);

    }

}

结果:

asp.net自定义控件

更多自定义控件的源代码

http://www.cnblogs.com/liuwj/admin/Files.aspx

你可能感兴趣的:(asp.net)