Define properties for user controls : Properties « Custom Controls « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default" %>
<%@ Register Src="Control.ascx" TagName="Message" TagPrefix="x" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Testing a User Control</title>
</head>
<body>
    <div id="pageAbstract">
            The page puts ASP.NET user controls through their paces.
    </div>
    
    <div id="pageContent">
        <form id="form1" runat="server">
            <x:Message ID="Message1" runat="server" />
        </form>
    </div>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Message1.ForeColor = "blue";
        Message1.Text = "Hello world";
    }
}


File: Control.ascx

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

<span style="color:<%= ForeColor%>"><%= Text%></span> 

File: Control.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default : System.Web.UI.UserControl
{
    public string ForeColor;
    public string Text;
}








14.12.Properties
14.12.1.Define properties for user controls