User control with readonly properties : Property « User Control and Master Page « ASP.Net






User control with readonly properties

<%@ Page %>
<%@ Register TagPrefix="uc1" TagName="header" Src="Control.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>User Controls</title>
    <Script language="C#" Runat="Server">
      private void Page_Load(object sender, System.EventArgs e)
    {
      header1.Author = "Snoopy";
      FooterLabel.Text = "Rendered " + header1.RenderDate;
    }
    </Script>
  </HEAD>
  <body>
    <form id="dependingdropdowns" method="post" runat="server">
      <uc1:header id="header1" runat="server" Title="A Tale of Mystery"></uc1:header>
      <p>It was a dark and stormy night.</p>
      <hr/>
      <asp:Label ID="FooterLabel" Runat="server" />
    </form>
  </body>
</HTML>

File: Control.ascx

<%@ Control Language="c#" %>
<script runat="server">
private string title;
private string author;
private DateTime renderDate = System.DateTime.Now;

public string Title {
  get {
    return title;
  }
  set {
    title = value;
  }
}
public string Author {
  get {
    return author;
  }
  set {
    author = value;
  }
}

// ReadOnly

public DateTime RenderDate {
  get {
    return renderDate;
  }
}

private void Page_Load(object sender, EventArgs e)
{
  TitleLabel.Text = Title;
  AuthorLabel.Text = Author;  
}
</script>
<h1><asp:Label id="TitleLabel" runat="server" /></h1>
<h3><asp:Label id="AuthorLabel" runat="server" /></h1>

 








Related examples in the same category

1.Creating ViewState-Enabled Control Properties (C#)
2.Creating ViewState-Enabled Control Properties (VB)