C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to Create and Use a simple Custom Control


Posted Date: 25 Jul 2008    Resource Type: Articles    Category: .NET Framework
Author: VidhyaMember Level: Gold    
Rating: 1 out of 5Points: 12



This article enlists the procedure to create and use a simple custom control that extends from System.Web.UI.Control using Visual Studio

1. Start Visual Studio.
2. Create a class library project, and give it a name, for example, CustomServerControlsLib.
3. Add a source file to the project, for example, SimpleServerControl.cs.
4. Include the reference of the System.Web namespace in the references section.
5. Check whether the following namespaces are included in the SimpleServerControl.cs file.System


System.Collections
System.ComponentModel
System.Data
System.Web
System.Web.SessionState
System.Web.UI
System.Web.UI.WebControls


6. Inherit the SimpleServerControls class with the Control base class. public class SimpleServerControl : Control

7. Override the Render method to write the output to the output stream.


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


Note:The HtmlTextWriter class has the functionality of writing HTML to a text stream. The Write method of the HtmlTextWriter class outputs the specified text to the HTTP response stream and is the same as the Response.Write method.

8. Compile the class library project. It will generate the DLL output.

9. Open an existing or create a new ASP.NET Web application project.

10. Add a Web Forms page where the custom control can be used.

11. Add a reference to the class library in the references section of the ASP.NET project.

12. Register the custom control on the Web Forms page.


<%@ Register TagPrefix="CC " Namespace=" CustomServerControlsLib " Assembly="CustomServerControlsLib " %>


13. To instantiate or use the custom control on the Web Forms page, add the following line of code in the tags.


<form id="Form1" method="post" runat="server">
<CC:SimpleServerControl id="ctlSimpleControl" runat="server">
</CC:SimpleServerControl >
</form>


Note In this code, SimpleServerControl is the control class name inside the class library.

14. Run the Web Forms page, and you will see the output from the custom control.



Responses

Author: Venkat    04 Sep 2008Member Level: Silver   Points : 1
Hi vidhya,

I cant understand ur code.So plz can u give brief explanation.Thanks

Regards
Venkat


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Use Custom Control  .  How to create and use a simple custom control in .NET  .  How to create and use a simple custom control in VISUAL STUDIO  .  Custom Controls in .NET  .  Custom control  .  Create custom control  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Creating User Defined Functions Using Managed Code For SQL Server 2005
Previous Resource: About Delegates and Anonymous methods in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use