ASP.Net/User Control and Master Page/Property — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:53, 26 мая 2010

Creating ViewState-Enabled Control Properties (C#)

<%@ Page language="C#" %>
<%@ Register TagPrefix="Control" Namespace="Control" Assembly="Control" %>
<script language="C#" runat="server">
void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    Random rand = new Random();
    ViewStateControl1.Text = rand.Next(1,100).ToString();
  }
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
  <head>
    <title>Creating ViewState-Enabled Control Properties</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <Control:ViewStateControl id="ViewStateControl1" runat="server" />
      <asp:linkbutton text="PostBack test" runat="server" />
    </form>
  </body>
</html>
File: Control.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ruponentModel;
namespace Control
{
  [DefaultProperty("Text"), 
  ToolboxData("<{0}:ViewStateControl runat=server></{0}:ViewStateControl>")]
  public class ViewStateControl : System.Web.UI.WebControls.WebControl
  {
    public string Text 
    {
      get
      {
        string text = (string) ViewState["Text"];
        return (text == null)? string.Empty : text;
      }
      set
      {
        ViewState["Text"] = value;
      }
    }
    protected override void Render(HtmlTextWriter writer)
    {
      writer.Write(Text);
    }
  }
}



Creating ViewState-Enabled Control Properties (VB)

<%@ Page language="VB" %>
<%@ Register TagPrefix="Control" Namespace="Control" Assembly="Control" %>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
  If Not IsPostBack Then
    Dim RandomGenerator As Random
    RandomGenerator = New Random(DateTime.Now.Millisecond)
    ViewStateControl1.Text = RandomGenerator.Next(1,100)
  End If
End Sub
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
  <body ms_positioning="GridLayout">
    <form id="Form1" method="post" runat="server">
      <Control:viewstatecontrol id="ViewStateControl1" runat="server" />
      <asp:linkbutton text="PostBack test" runat="server" id="Linkbutton1" />
    </form>
  </body>
</html>

File: Default.vb
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ruponentModel
Namespace Control
    Public Class ViewStateControl
        Inherits System.Web.UI.WebControls.WebControl
        Public Property Text() As String
            Get
                Dim _text As String = CStr(ViewState("Text"))
                If _text Is Nothing Then
                    Return String.Empty
                Else
                    Return _text
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("Text") = Value
            End Set
        End Property
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            writer.Write(Text)
        End Sub
    End Class
End Namespace



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>