no need to allocate new array
[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 using System;
12 using System.ComponentModel;
13 using System.Text;
14
15 namespace System.Web.UI {
16
17         [ToolboxItem(false)]
18         public sealed class DataBoundLiteralControl : Control
19         {
20                 private string [] staticLiterals;
21                 private string [] dataBoundLiterals;
22                 
23                 public DataBoundLiteralControl (int staticLiteralsCount,
24                                                 int dataBoundLiteralCount)
25                 {
26                         staticLiterals = new string [staticLiteralsCount];
27                         dataBoundLiterals = new string [dataBoundLiteralCount];
28                         PreventAutoID ();
29                 }
30
31                 public string Text {
32                         get {
33                                 StringBuilder text = new StringBuilder ();
34                                 int stLength = staticLiterals.Length;
35                                 int dbLength = dataBoundLiterals.Length;
36                                 int max = (stLength > dbLength) ? stLength : dbLength;
37                                 for (int i = 0; i < max; i++){
38                                         if (i < stLength)
39                                                 text.Append (staticLiterals [i]);
40                                         if (i < dbLength)
41                                                 text.Append (dataBoundLiterals [i]);
42                                 }
43
44                                 return text.ToString ();
45                         }
46                 }
47
48                 protected override ControlCollection CreateControlCollection ()
49                 {
50                         return new EmptyControlCollection (this);
51                 }
52
53                 protected override void LoadViewState (object savedState)
54                 {
55                         if (savedState != null) {
56                                 Array source = (Array) savedState;
57                                 if (source.Length == dataBoundLiterals.Length)
58                                         source.CopyTo (dataBoundLiterals, 0);
59                         }
60                 }
61
62                 protected override void Render (HtmlTextWriter output)
63                 {
64                         output.Write (Text);
65                 }
66
67                 protected override object SaveViewState ()
68                 {
69                         if (dataBoundLiterals.Length == 0)
70                                 return null;
71                         return dataBoundLiterals;
72                 }
73
74                 public void SetDataBoundString (int index, string s)
75                 {
76                         dataBoundLiterals [index] = s;
77                 }
78
79                 public void SetStaticString (int index, string s)
80                 {
81                         staticLiterals [index] = s;
82                 }
83         }
84 }
85