Docstoc

Custom Controls (PDF)

Document Sample
Custom Controls (PDF) Powered By Docstoc
					Custom Controls

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

Custom Controls are User-Defined classes to

create Server Controls.



2 - Types:



1. Simple Custom Controls

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

          - Minimal User Interface

          - Complete B.Logic.



2. Composite Custom Controls

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

          - Complete User Interface using

          Server Control classes from System.Web.UI.WebControls.

          - Complete B.Logic.



---

System.Web.UI Namespace

          - Control class             // to be inherited

[ or ]

System.Web.UI.WebControls Namespace

          - WebControl class          // to be inherited



----

Differences between User Controls & Custom Controls
----------------------------------------------------------------

User Controls

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

1. .ascx

           // Server Controls can be put by drag-n-drop

           // in a user control file[.ascx].



           @Control

           Cannot be requested directly by a client

           Registered in a web form [.aspx/.ascx/.master]



           <%@ Register Src="Banner.ascx"

                                TagName="Banner"

                                TagPrefix="abc" %>

           All user-controls have to use one each Register directive.



           <abc:Banner id="T1" runat="Server" />



2. Complied at run-time. [as a part of .aspx page]



3. Limited to one application only.



Custom Controls

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

1. *.cs => Bin\MyControls.dll [Assembly]

           // Custom Control Classes are to be written.
           Class // .cs

           An assembly cannot be requested directly by a client

           The Assembly should be Registered in a web form[.aspx/.ascx/.master].



           <%@ Register Assembly="MyControls"

                            Namespace="B115"

                            TagPrefix="abc" %>

           A single Register directive can use all the classes

           belong to the same namespace of the assembly.



           <abc:ClassName id="C1" runat="server" />



2. Compiled at Build - Time.



3. A custom control assembly can be made "Shared".

So that the assembly can be used by multiple applications

within the server.



--------

System.Web.UI Namespace

- Control class

           - Virtual Methods

           protected virtual void Render(HtmlTextWriter htw){}

           protected virtual void CreateChildControls() {}



[ or ]

System.Web.UI.WebControls Namespace
- WebControl class [Style properties are also inherited]

          - Virtual Methods

          protected virtual void Render(HtmlTextWriter htw){}

          protected virtual void CreateChildControls() {}



-----



A 'virtual member' of a 'Base Class' can be overriden

in the 'Derived Class' by using 'override' keyword.



-----

Simple Custom Control

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



MyControl.cs

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

using System.Web.UI;



namespace B115 {



public class MyControl : Control {



protected override void Render(HtmlTextWriter htw) {

          htw.Write("<h1 style=color:red>My First Control</h1>");

}



} // class
} // namespace



----



A.aspx

----------

<%@ Page . . . . %>

<%@ Register Assembly="MyAppB115"

                    Namespace="B115"

                    TagPrefix="abc" %>

<body>

<form runat=server >



             <abc:MyControl id="Mc1" runat=server />



</form>

</body>



-----




<abc:MyLabel id="" runat=server



             MyText="Welcome to Simple Controls"

             MyColor="Green"

             MySize="4"    // 1-7
          />



-------



<abc:AddNumbers id="Ad1" runat="server"



          FirstNumber="100"         // double

          SecondNumber="200" // double

          IsBold="true | false"     // bool

          />



100 + 200 = 300



---



<abc:CountRows id="Cr1" runat="server"

          //NewConnection=""

          Database="NewDB"

          Table="Users"

          />



Total Rows:77



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



Composite Custom Controls

---------------------------------
                 • Complete UI using server control class

                 System.Web.UI.WebControls namespace



                 • Complete B.Logic



       namespace B115

       {

       public class MyAdd : Control, INamingContainer

       {

       //UI

       protected override void CreateChildControls()

       {

       base.CreateChildControls();



       }



       // Events



 } // class

} // namespace



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

       Use of INamingContainer interface // no member

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

       one object of MyAdd at server



       But, Page contains two instances
MyAdd[0]

          txt1   // 10

          txt2   // 20

          btn

          lbl    // 30



MyAdd[1]

          txt1

          txt2

          btn

          lbl

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

				
DOCUMENT INFO
Shared By:
Stats:
views:34
posted:2/23/2012
language:
pages:9
Description: Custom Controls