Tuesday, March 11, 2014

Asp.NET User Control

User controls behaves like miniature ASP.Net pages, or web forms, which could be used by many other pages. These are derived from the System.Web.UI.UserControl class.

Here is the example for understanding the user control in asp.NET :-


create a new project.
right click on the project name in solution explorer.
click on add new item.
select Web User Control and add it.

write the following code in the file WebUserControl.ascx


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

<table>
<tr>
<td align="center"
        style="font-family: 'Arial Black'; font-size: large; font-weight: lighter; font-style: normal; color: #008080"> CEO at CandyFry.</td>
</tr>
<tr>
<td align="center"
        style="font-family: 'Arial Black'; font-size: large; font-weight: lighter; font-style: normal; color: #008080"> Living in Surat,Gujarat. </td>
</tr>
</table>

Then again add new item -> select web form  and add it :-

write the following code in the file Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="~/WebUserControl.ascx" TagName="mahi" TagPrefix="mahi1" %>

<!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>User Control Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"
         Text="Mahi Babariya" ForeColor="#FF0066"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Info" ForeColor="#993399" />
     
       </div>
       <br />
       <mahi1:mahi ID="footer1" runat="server" />
    </form>
</body>

</html>

save both file and run it in the browser.

Output:-



No comments:

Post a Comment