January 15, 2015

Asp.net server controls from scratch

we can then build reusable visual components for our web application’s user interface by creating our own controls.We can add this custom .dll in GAC and can share with other control too.We can create a custom control that inherits from another server-side control and then extend that control. We can also share a custom control among projects.

step to create project
1) create project -Asp.net server control -this is used to create dll

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

namespace ServerControl1
{
    [ToolboxData("<{0}:VideoPlayer runat=server></{0}:VideoPlayer>")]
    public class VideoPlayer: WebControl
    {
        private string _Mp4Url;
        public string Mp4Url
        {
            get { return _Mp4Url; }
            set { _Mp4Url = value; }
        }

        private string _OggUrl = null;
        public string OggUrl
        {
            get { return _OggUrl; }
            set { _OggUrl = value; }
        }

        private string _Poster = null;
        public string PosterUrl
        {
            get { return _Poster; }
            set { _Poster = value; }
        }

        private bool _AutoPlay = false;
        public bool AutoPlay
        {
            get { return _AutoPlay; }
            set { _AutoPlay = value; }
        }

        private bool _Controls = true;
        public bool DisplayControlButtons
        {
            get { return _Controls; }
            set { _Controls = value; }
        }

        private bool _Loop = false;
        public bool Loop
        {
            get { return _Loop; }
            set { _Loop = value; }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
            output.AddAttribute(HtmlTextWriterAttribute.Width, this.Width.ToString());
            output.AddAttribute(HtmlTextWriterAttribute.Height, this.Height.ToString());

            if (DisplayControlButtons == true)
            {
                output.AddAttribute("controls", "controls");
            }

            if (PosterUrl != null)
            {
                output.AddAttribute("poster", PosterUrl);
            }

            if (AutoPlay == true)
            {
                output.AddAttribute("autoplay", "autoplay");
            }

            if (Loop == true)
            {
                output.AddAttribute("loop", "loop");
            }
            output.RenderBeginTag("video");
            if (OggUrl != null)
            {
                output.AddAttribute("src", OggUrl);
                output.AddAttribute("type", "video/ogg");
                output.RenderBeginTag("source");
                output.RenderEndTag();
            }

            if (Mp4Url != null)
            {
                output.AddAttribute("src", Mp4Url);
                output.AddAttribute("type", "video/mp4");
                output.RenderBeginTag("source");
                output.RenderEndTag();
            }
            output.RenderEndTag();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.RenderContents(writer);
        }
       
    }
   
}
build it.it will create dll.add this dll in Toolbox and use in other project.

2) Next required any asp.net project to use this dll and show on page.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="ServerControl1" Namespace="ServerControl1" TagPrefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:VideoPlayer ID="VideoPlayer1" runat="server" Mp4Url="http://techslides.com/demos/sample-videos/small.mp4" OggUrl="http://techslides.com/demos/sample-videos/small.ogv" Width="400" Height="400"  />
    </div>
    </form>
</body>
</html>

run on Google chrome browser.