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>