-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathDataBoundLiteralControl.cs
254 lines (192 loc) · 6.89 KB
/
DataBoundLiteralControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//------------------------------------------------------------------------------
// <copyright file="DataBoundLiteralControl.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* Control that holds databinding expressions and literals
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.UI {
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
using System.Text;
using System.Security.Permissions;
using System.Web.Util;
internal class DataBoundLiteralControlBuilder : ControlBuilder {
internal DataBoundLiteralControlBuilder() {
}
internal void AddLiteralString(string s) {
Debug.Assert(!InDesigner, "!InDesigner");
// Make sure strings and databinding expressions alternate
object lastBuilder = GetLastBuilder();
if (lastBuilder != null && lastBuilder is string) {
AddSubBuilder(null);
}
AddSubBuilder(s);
}
internal void AddDataBindingExpression(CodeBlockBuilder codeBlockBuilder) {
Debug.Assert(!InDesigner, "!InDesigner");
// Make sure strings and databinding expressions alternate
object lastBuilder = GetLastBuilder();
if (lastBuilder == null || lastBuilder is CodeBlockBuilder) {
AddSubBuilder(null);
}
AddSubBuilder(codeBlockBuilder);
}
internal int GetStaticLiteralsCount() {
// it's divided by 2 because half are strings and half are databinding
// expressions). '+1' because we always start with a literal string.
return (SubBuilders.Count+1) / 2;
}
internal int GetDataBoundLiteralCount() {
// it's divided by 2 because half are strings and half are databinding
// expressions)
return SubBuilders.Count / 2;
}
}
/// <devdoc>
/// <para>Defines the properties and methods of the DataBoundLiteralControl class. </para>
/// </devdoc>
[
ToolboxItem(false)
]
public sealed class DataBoundLiteralControl : Control, ITextControl {
private string[] _staticLiterals;
private string[] _dataBoundLiteral;
private bool _hasDataBoundStrings;
/// <internalonly/>
public DataBoundLiteralControl(int staticLiteralsCount, int dataBoundLiteralCount) {
_staticLiterals = new string[staticLiteralsCount];
_dataBoundLiteral = new string[dataBoundLiteralCount];
PreventAutoID();
}
/// <internalonly/>
public void SetStaticString(int index, string s) {
_staticLiterals[index] = s;
}
/// <internalonly/>
public void SetDataBoundString(int index, string s) {
_dataBoundLiteral[index] = s;
_hasDataBoundStrings = true;
}
/// <devdoc>
/// <para>Gets the text content of the data-bound literal control.</para>
/// </devdoc>
public string Text {
get {
StringBuilder sb = new StringBuilder();
int dataBoundLiteralCount = _dataBoundLiteral.Length;
// Append literal and databound strings alternatively
for (int i=0; i<_staticLiterals.Length; i++) {
if (_staticLiterals[i] != null)
sb.Append(_staticLiterals[i]);
// Could be null if DataBind() was not called
if (i < dataBoundLiteralCount && _dataBoundLiteral[i] != null)
sb.Append(_dataBoundLiteral[i]);
}
return sb.ToString();
}
}
/// <internalonly/>
protected override ControlCollection CreateControlCollection() {
return new EmptyControlCollection(this);
}
/// <internalonly/>
/// <devdoc>
/// <para>Loads the previously saved state. Overridden to synchronize Text property with
/// LiteralContent.</para>
/// </devdoc>
protected override void LoadViewState(object savedState) {
if (savedState != null) {
_dataBoundLiteral = (string[]) savedState;
_hasDataBoundStrings = true;
}
}
/// <internalonly/>
/// <devdoc>
/// <para>The object that contains the state changes. </para>
/// </devdoc>
protected override object SaveViewState() {
// Return null if we didn't get any databound strings
if (!_hasDataBoundStrings)
return null;
// Only save the databound literals to the view state
return _dataBoundLiteral;
}
/// <internalonly/>
protected internal override void Render(HtmlTextWriter output) {
int dataBoundLiteralCount = _dataBoundLiteral.Length;
// Render literal and databound strings alternatively
for (int i=0; i<_staticLiterals.Length; i++) {
if (_staticLiterals[i] != null)
output.Write(_staticLiterals[i]);
// Could be null if DataBind() was not called
if (i < dataBoundLiteralCount && _dataBoundLiteral[i] != null)
output.Write(_dataBoundLiteral[i]);
}
}
/// <internalonly/>
/// <devdoc>
/// <para>Implementation of TextControl.Text property. </para>
/// </devdoc>
string ITextControl.Text {
get {
return Text;
}
set {
throw new NotSupportedException();
}
}
}
/// <devdoc>
/// <para>Simpler version of DataBoundLiteralControlBuilder, used at design time. </para>
/// </devdoc>
[
DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + AssemblyRef.SystemDesign),
ToolboxItem(false)
]
public sealed class DesignerDataBoundLiteralControl : Control {
private string _text;
public DesignerDataBoundLiteralControl() {
PreventAutoID();
}
/// <devdoc>
/// <para>Gets or sets the text content of the data-bound literal control.</para>
/// </devdoc>
public string Text {
get {
return _text;
}
set {
_text = (value != null) ? value : String.Empty;
}
}
protected override ControlCollection CreateControlCollection() {
return new EmptyControlCollection(this);
}
/// <devdoc>
/// <para>Loads the previously saved state. Overridden to synchronize Text property with
/// LiteralContent.</para>
/// </devdoc>
protected override void LoadViewState(object savedState) {
if (savedState != null)
_text = (string) savedState;
}
/// <devdoc>
/// <para>Saves any state that was modified after the control began monitoring state changes.</para>
/// </devdoc>
protected internal override void Render(HtmlTextWriter output) {
output.Write(_text);
}
/// <devdoc>
/// <para>The object that contains the state changes. </para>
/// </devdoc>
protected override object SaveViewState() {
return _text;
}
}
}