2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web.UI / DataBoundLiteralControl.cs
1 //
2 // System.Web.UI.DataBoundLiteralCOntrol.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.ComponentModel;
34 using System.Text;
35
36 namespace System.Web.UI {
37
38         [ToolboxItem(false)]
39         public sealed class DataBoundLiteralControl : Control
40         {
41                 private string [] staticLiterals;
42                 private string [] dataBoundLiterals;
43                 
44                 public DataBoundLiteralControl (int staticLiteralsCount,
45                                                 int dataBoundLiteralCount)
46                 {
47                         staticLiterals = new string [staticLiteralsCount];
48                         dataBoundLiterals = new string [dataBoundLiteralCount];
49                         PreventAutoID ();
50                 }
51
52                 public string Text {
53                         get {
54                                 StringBuilder text = new StringBuilder ();
55                                 int stLength = staticLiterals.Length;
56                                 int dbLength = dataBoundLiterals.Length;
57                                 int max = (stLength > dbLength) ? stLength : dbLength;
58                                 for (int i = 0; i < max; i++){
59                                         if (i < stLength)
60                                                 text.Append (staticLiterals [i]);
61                                         if (i < dbLength)
62                                                 text.Append (dataBoundLiterals [i]);
63                                 }
64
65                                 return text.ToString ();
66                         }
67                 }
68
69                 protected override ControlCollection CreateControlCollection ()
70                 {
71                         return new EmptyControlCollection (this);
72                 }
73
74                 protected override void LoadViewState (object savedState)
75                 {
76                         if (savedState != null) {
77                                 Array source = (Array) savedState;
78                                 if (source.Length == dataBoundLiterals.Length)
79                                         source.CopyTo (dataBoundLiterals, 0);
80                         }
81                 }
82
83                 protected override void Render (HtmlTextWriter output)
84                 {
85                         output.Write (Text);
86                 }
87
88                 protected override object SaveViewState ()
89                 {
90                         if (dataBoundLiterals.Length == 0)
91                                 return null;
92                         return dataBoundLiterals;
93                 }
94
95                 public void SetDataBoundString (int index, string s)
96                 {
97                         dataBoundLiterals [index] = s;
98                 }
99
100                 public void SetStaticString (int index, string s)
101                 {
102                         staticLiterals [index] = s;
103                 }
104         }
105 }
106