Library tutorials & articles
ASP.NET Controls Explained: Part 2/2
- Introduction
- Custom Controls VS Components
- Components
- Conclusion
Custom Controls VS Components
It's time to introduce custom controls and components. Custom controls provide
programmers with reusable HTML outputting code that involves a small amount of
processing. They are accessed through a web page (.aspx), using custom tags,
just like user controls. All server controls can be thought of as custom controls
that have been pre-made by Microsoft.
Components on the other hand provide programmers with reusable application logic
that involves some fairly intensive processing. They are accessible from a code
behind page or between <script> and </script> tags in a web page.
All base classes and objects developed by Microsoft can be considered pre-made
components.
Although both custom controls and components can do exactly the same things,
their uses will become clearer as you have more experience with them. For example,
you could make a menu bar using components and load a method in a component that
makes the menu load in the Page_Load class, but it is much easier to use custom
controls in an aspx page as custom tags, and place them wherever we want the
menu to appear.
Using a custom control
A good use of components is to turn functions contained within application logic
code into reusable components. Let's have a look at an example of using a custom
control. We will make a custom control that takes a parameter and displays that
parameter in a large font. Start off by opening notepad and entering following
code. Save the file as customc.cs. using System;
using System.Web;
using System.Web.UI;
namespace devArticles
{
public class CustomC : Control
{
string tempText;
public string Text
{
get {return tempText;}
set {tempText = value;}
}
protected override void Render(HtmlTextWriter writer)
{
if (tempText == null)
{
tempText = "Default Text";
}
writer.Write ("<h1>" + tempText + "</h1>");
}
}
}
Open up another notepad window and save the following code as customc.aspx: <%@ Register TagPrefix="devArticles" Namespace="devArticles"
Assembly="CustomC"%>
<html>
<head>
<title>Code Behind Example</title>
</head>
<body>
<devArticles:CustomC runat= "server" Text = "Title"/><br>
<devArticles:CustomC runat= "server" />
</body>
</html>
Using the command line, enter the following commands to compile our custom control:
md bin
csc /t:library /out:bin\customc.dll /r:System.dll,System.Web.Dll customc.cs
Let's take a look at the details of the code shown in the examples above. public class CustomC : Control
public string Text
{
get {return tempText;}
set {tempText = value;}
}
We start by creating a class that is derived from the "Control" base class. Next,
we have a public property named "Text". We can use the get and set accessor methods
to modify and set the value of the string variable tempText variable, which exists
in our class. protected override void Render(HtmlTextWriter writer)
In the line above, we over ride the inherited method named Render. We declare
the method as protected, because we don't want people to access if from outside
of the control. The HtmlTextWriter is an object that we automatically receive
from server. It allows us to output text to the web browser. In an aspx page,
every tag is written to use this object when it is displayed. if (tempText == null)
{
tempText = "Default Text";
}
writer.Write ("<h1>" + tempText + "</h1>");
The code above makes sure that the tempText value can never be empty. If it is
empty (ie the get accessor of the Text property hasn’t been called), then it
is set to "Default Text". Lastly, we use the Write method of our HtmlTextWriter
object to output its value between a <h1> tag to the browser.
Let's now take a look at the aspx page: <%@ Register TagPrefix ="devArticles" Namespace="devArticles"
Assembly="CustomC"%>
We start off by registering a new user control, except we use the "Assembly"
keyword instead of "TagName", and "Namespace" instead of the file location, because
the server already knows where to search for the .dll files (in the /bin folder).
<devArticles:CustomC runat= "server" Text = "Title"/><br>
<devArticles:CustomC runat= "server" />
We have used the same custom control twice to show how that default value will
be assigned if one isn't specified. The first tag explicitly specifies a value
for our Text property, whereas the second one doesn't. Before this example will
work, we have to compile it using the C# compiler: csc /t:library /out:bin\customc.dll /r:System.dll,System.Web.Dll
customc.cs
In our call to the C# compiler, we specify the output directory for our files,
as well as the file names and namespace that we have used to create our files.
Finally, we specify the name of our source code file.
When I ran this example in my web browser, it looked like this:
And there we have it, our first custom control. Now that we can create and compile
custom controls, let's take a look at components.
Related articles
Related discussion
-
Gizmox Announces release of Visual WebGui SDK version 6.2.2
by Visual WebGui (0 replies)
-
Error 4 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
by lbargers (3 replies)
-
How to receive data in web server sending from GPRS modem
by AshokSingh (1 replies)
-
Working with frames in c# .net
by pulak2008 (0 replies)
-
Time Out Problem
by lorrainepetty (1 replies)
Related podcasts
-
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum) -
.net developer
in Rijswijk (€2K-€4K per annum)
Events coming up
-
Dec
3
An afternoon of SQL Server Data Services and ASP.NET Dynamic Data
Bradford, United Kingdom
This event is in association with Black Marble. In the morning Black Marble will be presenting on Microsoft "Oslo": The Future of Enterprise Applications. To find out more about this please follow the link on the right.
Can i print code in main page head tag by overriding render without adding any extra item in head tag.
I find this article really helpful for those who are starting to get familiar to asp controls.
Thanks!
This thread is for discussions of ASP.NET Controls Explained: Part 2/2.