How to create User Control in ASP.Net
User control is powerful functionality of ASP.NET. With the help of the user control you can reuse the Design as well as code in the application. Developing User control is similar to developing form in ASP.NET. User control is created in markup file with .ascx extension.
User control is powerful functionality of ASP.NET. With the help of the user control you can reuse the Design as well as code in the application. Developing User control is similar to developing form in ASP.NET. User control is created in markup file with .ascx extension.
<%@ Page Language="C#"%>
<!--First Step. You need to register the user control
which you are using, here src is the path to the user control, tagName and
tagPrefix are just text to declare the user control in the form-->
<%@ Register src="UserLogin.ascx"
tagname="UserLogin" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How
To Make User Controls In ASP.NET</title>
<script
language="C#" runat="server">
public void
Page_Load(Object sender, EventArgs e)
{
//Print
the time when the page loaded initially
Response.Write("The Page Loaded at:
" + DateTime.Now.ToLongTimeString());
}
</script>
<style
type="text/css">
.button
{
border-radius:4px 4px 4px 4px;
height:30px;
padding:5px;
font-size:14px;
background-color:#6ca21e;
color:#FFFFFF
}
body
{
background-color:#32373a;
color:#FFFFFF;
}
#mainBody
{
background-color:#FFFFFF;
height:100%;
color:#32373a;
}
.UCHelp
{
height:150px;margin-top:20px;
font-weight:bold;
}
.UserControlDiv
{
width:250px;margin:20px;padding:30px;border:1px solid
Gray;background-color:#fdd136;
}
</style>
</head>
<body>
<form
id="form1" runat="server">
<div
id="mainBody">
<h1>
How To
Make User Controls In ASP.NET
</h1>
<!--
Declaring the User control first time. The format is {tagprefix}:{tagname}
which are defined above -->
<uc1:UserLogin ID="UserLogin1" runat="server"
/>
<div
class="UCHelp">
A user
controls differs from an ASP.NET Web page in these ways:
<ul>
<li>
File name extension for a user control is .ascx.
</li>
<li>
ASP.NET uses @ Page directive, whereas user control contains an @
Control directive that defines configuration and other properties.
</li>
<li>
User controls cannot run as stand-alone
files. Instead, you must add them to ASP.NET pages, as you would any control.
</li>
<li>
The user control does not have html, body, or form elements in it. These
elements must be in the hosting page.
</li>
</ul>
</div>
<!-- Again
reusing the same user controls, shows how it can be reused, so we can again
declare the user control.-->
<uc1:UserLogin ID="UserLogin2" runat="server"
/>
</div>
</form>
</body>
</html>
No comments:
Post a Comment