1 // Book.cs
2 using System;
3 using System.ComponentModel;
4 using System.Security.Permissions;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 
9 namespace Samples.AspNet.CS.Controls
10 {
11     [
12     AspNetHostingPermission(SecurityAction.Demand,
13         Level = AspNetHostingPermissionLevel.Minimal),
14     AspNetHostingPermission(SecurityAction.InheritanceDemand,
15         Level=AspNetHostingPermissionLevel.Minimal),
16     DefaultProperty("Title"),
17     ToolboxData("<{0}:Book runat=\"server\"> </{0}:Book>")
18     ]
19     public class Book : WebControl
20     {
21         private Author authorValue;
22         private String initialAuthorString;
23 
24         [
25         Bindable(true),
26         Category("Appearance"),
27         DefaultValue(""),
28         Description("The name of the author."),
29         DesignerSerializationVisibility(
30             DesignerSerializationVisibility.Content),
31         PersistenceMode(PersistenceMode.InnerProperty),
32         ]
33         public virtual Author Author
34         {
35             get
36             {
37                 if (authorValue == null)
38                 {
39                     authorValue = new Author();
40                 }
41                 return authorValue;
42             }
43 
44         }
45 
46         [
47         Bindable(true),
48         Category("Appearance"),
49         DefaultValue(BookType.NotDefined),
50         Description("Fiction or Not"),
51         ]
52         public virtual BookType BookType
53         {
54             get
55             {
56                 object t = ViewState["BookType"];
57                 return (t == null) ? BookType.NotDefined : (BookType)t;
58             }
59             set
60             {
61                 ViewState["BookType"] = value;
62             }
63         }
64 
65         [
66         Bindable(true),
67         Category("Appearance"),
68         DefaultValue(""),
69         Description("The symbol for the currency."),
70         Localizable(true)
71         ]
72         public virtual string CurrencySymbol
73         {
74             get
75             {
76                 string s = (string)ViewState["CurrencySymbol"];
77                 return (s == null) ? String.Empty : s;
78             }
79             set
80             {
81                 ViewState["CurrencySymbol"] = value;
82             }
83         }
84 
85 
86         [
87         Bindable(true),
88         Category("Appearance"),
89         DefaultValue("0.00"),
90         Description("The price of the book."),
91         Localizable(true)
92         ]
93         public virtual Decimal Price
94         {
95             get
96             {
97                 object price = ViewState["Price"];
98                 return (price  == null) ? Decimal.Zero : (Decimal)price;
99             }
100             set
101             {
102                 ViewState["Price"] = value;
103             }
104         }
105 
106         [
107         Bindable(true),
108         Category("Appearance"),
109         DefaultValue(""),
110         Description("The title of the book."),
111         Localizable(true)
112         ]
113         public virtual string Title
114         {
115             get
116             {
117                 string s = (string)ViewState["Title"];
118                 return (s == null) ? String.Empty : s;
119             }
120             set
121             {
122                 ViewState["Title"] = value;
123             }
124         }
125 
RenderContents(HtmlTextWriter writer)126         protected internal override void RenderContents(HtmlTextWriter writer)
127         {
128             writer.RenderBeginTag(HtmlTextWriterTag.Table);
129 
130             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
131             writer.RenderBeginTag(HtmlTextWriterTag.Td);
132             writer.WriteEncodedText(Title);
133             writer.RenderEndTag();
134             writer.RenderEndTag();
135 
136             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
137             writer.RenderBeginTag(HtmlTextWriterTag.Td);
138             writer.WriteEncodedText(Author.ToString());
139             writer.RenderEndTag();
140             writer.RenderEndTag();
141 
142             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
143             writer.RenderBeginTag(HtmlTextWriterTag.Td);
144             writer.WriteEncodedText(BookType.ToString());
145             writer.RenderEndTag();
146             writer.RenderEndTag();
147 
148             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
149             writer.RenderBeginTag(HtmlTextWriterTag.Td);
150             writer.Write(CurrencySymbol);
151             writer.Write("&nbsp;");
152             writer.Write(String.Format("{0:F2}", Price));
153             writer.RenderEndTag();
154             writer.RenderEndTag();
155 
156             writer.RenderEndTag();
157         }
158 
LoadViewState(object savedState)159         protected override void LoadViewState(object savedState)
160         {
161             base.LoadViewState(savedState);
162 
163             Author auth = (Author)ViewState["Author"];
164             if (auth != null)
165             {
166                 authorValue = auth;
167             }
168         }
169 
SaveViewState()170         protected override object SaveViewState()
171         {
172             if (authorValue != null)
173             {
174                 String currentAuthorString = authorValue.ToString();
175                 if (!(currentAuthorString.Equals(initialAuthorString)))
176                 {
177                     ViewState["Author"] = authorValue;
178                 }
179             }
180 
181             return base.SaveViewState();
182         }
183 
TrackViewState()184         protected override void TrackViewState()
185         {
186             if (authorValue != null)
187             {
188                 initialAuthorString = authorValue.ToString();
189             }
190             base.TrackViewState();
191         }
192 
193     }
194 }
195