Click here to Skip to main content
15,879,095 members
Articles / Web Development / ASP.NET

How to Develop a Simple Vertical Text Scroller Custom Server Control in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.42/5 (5 votes)
23 Jul 2009CPOL5 min read 37.9K   332   18  
Step by step on how to create a custom server control in ASP.NET 2.0
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Security.Permissions;
using System.Web.UI.Design;
using System.Drawing.Design;

namespace MyTextScroller
{
    /// <summary>
    /// Author Bryian Tan
    /// Text Scroller v1.1 
    /// </summary>
    [
        AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
        AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
        ParseChildren(true, "innerHTML"),
        DefaultProperty("innerHTML"),
        ToolboxData("<{0}:TextScroller runat=\"server\"> </{0}:TextScroller>")
        
    ]
    public class TextScroller : WebControl, INamingContainer
    {
        #region Public Properties

        //width of the div
        private Unit width = Unit.Pixel(260);
        public override Unit Width
        {
            get { return width; }
            set { EnsureChildControls(); width = value; }
        }

        //height of the div
        private Unit height = Unit.Pixel(160);
        public override Unit Height
        {
            get { return height; }
            set { EnsureChildControls(); height = value; }
        }

        //content
        private string _innerHTML = "Your Content here";

        [   
            Browsable(true), //display this property in designer
            Category("Appearance"),
            DefaultValue("Your Content Here"),
            Description("The Content to Display."), 
            PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public virtual string innerHTML
        {
            get
            {
                return _innerHTML;
            }
            set
            {
                EnsureChildControls(); 
                _innerHTML = value; 
            }
        }

        #endregion 

        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);

            //access embedded Resource
            this.Page.ClientScript.RegisterClientScriptInclude(
                        this.GetType(), "TextScroller",
                        Page.ClientScript.GetWebResourceUrl(this.GetType(),
                        "MyTextScroller.TextScroller.js"));

            // create the style sheet control and put it in the document header
            string csslink = @"<link rel='stylesheet' type='text/css' href='" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyTextScroller.TextScroller.css") + "' />";
            LiteralControl include = new LiteralControl(csslink);

            this.Page.Header.Controls.Add(include);

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter output)
        {
            EnsureChildControls();

            output.Write(GenerateDiv());
        }

        private string GenerateDiv()
        {
            StringBuilder b = new StringBuilder();
            ClientScriptManager cs = Page.ClientScript;
            Type rsType = this.GetType();

            b.Append("<div style=\"width:");
            b.Append(width.Value);
            b.Append("px;width/**/:");
            b.Append(width.Value + 20);
            b.Append("px !important; height: ");
            b.Append(Height.Value);
            b.Append("px; \" class = \"MS_vmarquee_div\">");
            b.Append("<div id=\"marqueecontainer\" style=\"float:left; width:");
            b.Append(width.Value - 10);
            b.Append("px;width/**/: ");
            b.Append(width.Value - 10);
            b.Append(@"px !important;\width:");
            b.Append(width.Value);
            b.Append(@"px;w\idth:");
            b.Append(width.Value - 10);
            b.Append("px !important; height:");
            b.Append(Height.Value - 5);
            b.Append(@"px; hei\ght:");
            b.Append(Height.Value - 10);
            b.Append("px;\" class=\"MS_marqueecontainer\" onMouseover=\"copyspeed=pausespeed\" onMouseout=\"copyspeed=marqueespeed\">");

            //content
            b.Append("<div id=\"vmarquee\" class=\"MS_vmarquee_content\">");
            b.Append(innerHTML);
            b.Append("</div>"); //vmarquee
            b.Append("</div>"); //marqueecontainer       
            b.Append(" <div class=\"floatright center\">");

            //up button
            b.Append(" <img alt=\"U\" src=\"");
            b.Append(cs.GetWebResourceUrl(rsType, "MyTextScroller.images.nav-arrow-up.gif"));
            b.Append("\" width=\"13\" height=\"13\" border=\"0\" id=\"MS_rollfirst\" ");
            b.Append("onmouseover=\"document.getElementById('MS_rollfirst').src='");
            b.Append(cs.GetWebResourceUrl(rsType, "MyTextScroller.images.nav-arrow-up-hover.gif"));
            b.Append("';pressup();\"");
            b.Append("onmouseout=\"document.getElementById('MS_rollfirst').src='");
            b.Append(cs.GetWebResourceUrl(rsType, "MyTextScroller.images.nav-arrow-up.gif"));
            b.Append("';mouse_out();\">");

            b.Append("<br>");

            //down button
            b.Append("<img  alt=\"D\" src=\"");
            b.Append(cs.GetWebResourceUrl(rsType, "MyTextScroller.images.nav-arrow-down.gif"));
            b.Append("\" width=\"13\" height=\"13\" border=\"0\" id=\"MS_rollfirst2\" ");
            b.Append("onmouseover=\"document.getElementById('MS_rollfirst2').src='");
            b.Append(cs.GetWebResourceUrl(rsType, "MyTextScroller.images.nav-arrow-down-hover.gif"));
            b.Append("';pressdown();\"");
            b.Append(" onmouseout=\"document.getElementById('MS_rollfirst2').src='");
            b.Append(cs.GetWebResourceUrl(rsType, "MyTextScroller.images.nav-arrow-down.gif"));
            b.Append("';mouse_out();\">	");

            //hidden field
            b.Append("<input id=\"track\" name=\"MS_track\" type=\"hidden\" value=\"\" />");
            b.Append("</div>"); //floatright

            b.Append("</div>"); //MS_vmarquee_div

            return b.ToString();
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e); 
        }    

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I have over 10 years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

Comments and Discussions