Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

What is custom control?
For which purpose is it useful?
Could you please explain to me with a proper example?
Posted
Updated 5-Jun-11 21:31pm
v3
Comments
Dalek Dave 6-Jun-11 3:32am    
Edited for Syntax and Readability.

 
Share this answer
 
v2
Comments
Dalek Dave 6-Jun-11 3:32am    
Nice Link
 
Share this answer
 
Comments
Dalek Dave 6-Jun-11 3:32am    
Good link
XML
User controls are custom, reusable controls. They are more or less like the Web forms, with similar programming model like a Web page. user controls in asp.net ends with an ".ascx" extension. Whereas Custom control are compiled class library program. They are .dll files.
You can create a .ascx file and can use it across your website.
for eg:
1. Create a "Test.ascx" file in your project and write the foll. code in your ascx file
    <script language="C#" runat="server">
        public void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Hello World!!!";
         }
    </script>
    <asp:Label id="label1" runat="server"/>
    <br><br>
    <asp:button id="button1" text="Hit" OnClick="button1_Click" runat="server" />
2. Now you can use this Test.ascx in a web page or an other .ascx file
    a.) First you need to register the user control in the page
        <%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>
    b.) you can now use the usercontrol as :
        <html>
           <body>
                 <form runat="server">
                        <UC:TestControl id="Test1" runat="server"/>
                 </form>
          </body>
        </html>
-----------------------------------------------------------------------------



Custom controls are compiled code components that execute on the server, expose the object model, and render markup text, such as HTML or XML, as a normal Web Form or user control does.

To write a custom control, you should directly or indirectly derive the new class from the System.Web.UI.Control class or from the System.Web.UI.WebControls.WebControl class.

You create a custom control if u want to change or extend the default functionality of
existing controls

 Controls provided by Telerik are the best examples of custom controls

for eg:
1.)
using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Web
using System.Web.UI
using System.Web.UI.WebControls


public class SimpleServerControl : Control

protected override void Render(HtmlTextWriter writer)
{
     writer.Write("Hello World from custom control");
}



2.) Now u need to add the reference for the generated dll in your project and then the register the custom controls in the page or .ascx file just like
usercontrols
    <%@ Register TagPrefix="CC " Namespace=" CustomServerControlsLib " Assembly="CustomServerControlsLib " %>


3.)
   <form id="Form1" method="post" runat="server">
    <CC:SimpleServerControl id="ctlSimpleControl" runat="server">
    </CC:SimpleServerControl >
</form>
 
Share this answer
 
Comments
Dalek Dave 6-Jun-11 3:32am    
Good Answer.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900