Wednesday, September 29, 2010

Creating a User Control in ASP.Net

Hello Friends lets work on User control in this post.

Going through the requirements programmers designed a format of editing a control and using that control in all over the project that is called User Control.

Lets start,

First of all

Create a New Web Site, and right click on the solution file and select Add New Item, then in Add New Item window select Web User Control and specify your desire Name in the Name feild and Click Add button.

Name:: sample

So you see a file with .ascx extension. In that file you see default code as shown below.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Sample.ascx.cs" Inherits="Sample" %>

So lets start writing some code here. You can add any number of controls. By adding any number of controls you need like .aspx page. I just show simple example how it works.

<p>
Welcome to CoolTuts friends, Number one Tutorial Website on WWW
</p>

Add this after the default code, you can also delete the default code and place this above code.

Just build and open .aspx page in which you want to place the usercontrol.

Here you need to add some code before <html> tag

<%@ Register TagPrefix="SomeTagText" TagName="SomeText" Src="~/Sample.ascx" %>

Here you need to add some code before <html> tag

Adding the line above html tag, then add user control on to the location where you desire.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<SomeTagText:SomeText runat="server" />
</div>
</form>
</body>
</html>

Debug and see your usercontrol on Web.

No comments:

Post a Comment