原来2.0里实现数据绑定控件这么简单!
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Text;
5
using System.Web;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
9
10
namespace WYN.WebControls
11
{
12
13
[ToolboxData("<{0}:FilterTextBox runat=server></{0}:FilterTextBox>")]
14
public class FilterTextBox : DataBoundControl
15
{
16
private ListItemCollection listSource = null;
17
protected ListItemCollection ListSource
18
{
19
get{
20
21
if (listSource == null)
22
listSource = new ListItemCollection();
23
24
return listSource;
25
}
26
27
}
28
29
private int selectedIndex = 0;
30
public int SelectedIndex
31
{
32
get
33
{
34
35
return selectedIndex;
36
}
37
set
38
{
39
selectedIndex = value;
40
41
if (selectedIndex < 0 || selectedIndex > ListSource.Count)
42
{
43
selectedIndex = 0;
44
}
45
46
if (listSource.Count != 0)
47
{
48
listSource[selectedIndex].Selected = true;
49
}
50
}
51
}
52
53
public string DataTextField
54
{
55
get
56
{
57
object o = ViewState["DataTextField"];
58
return ((o == null) ? string.Empty : (string)o);
59
}
60
set
61
{
62
ViewState["DataTextField"] = value;
63
if (Initialized)
64
{
65
OnDataPropertyChanged();
66
}
67
}
68
}
69
70
public string DataValueField
71
{
72
get
73
{
74
object o = ViewState["DataValueField"];
75
return ((o == null) ? string.Empty : (string)o);
76
}
77
set
78
{
79
ViewState["DataValueField"] = value;
80
if (Initialized)
81
{
82
OnDataPropertyChanged();
83
}
84
}
85
}
86
87
88
protected override void PerformSelect()
89
{
90
91
// Call OnDataBinding here if bound to a data source using the
92
// DataSource property (instead of a DataSourceID), because the
93
// databinding statement is evaluated before the call to GetData.
94
if (!IsBoundUsingDataSourceID)
95
{
96
OnDataBinding(EventArgs.Empty);
97
}
98
99
// The GetData method retrieves the DataSourceView object from
100
// the IDataSource associated with the data-bound control.
101
GetData().Select(CreateDataSourceSelectArguments(),
102
OnDataSourceViewSelectCallback);
103
104
// The PerformDataBinding method has completed.
105
RequiresDataBinding = false;
106
MarkAsDataBound();
107
108
// Raise the DataBound event.
109
OnDataBound(EventArgs.Empty);
110
}
111
private void OnDataSourceViewSelectCallback(System.Collections.IEnumerable retrievedData)
112
{
113
114
// Call OnDataBinding only if it has not already been
115
// called in the PerformSelect method.
116
if (IsBoundUsingDataSourceID)
117
{
118
OnDataBinding(EventArgs.Empty);
119
}
120
// The PerformDataBinding method binds the data in the
121
// retrievedData collection to elements of the data-bound control.
122
PerformDataBinding(retrievedData);
123
}
124
125
126
protected override void PerformDataBinding(System.Collections.IEnumerable retrievedData)
127
{
128
base.PerformDataBinding(retrievedData);
129
130
// If the data is retrieved from an IDataSource as an
131
// IEnumerable collection, attempt to bind its values to a
132
// set of TextBox controls.
133
if (retrievedData != null)
134
{
135
136
foreach (object dataItem in retrievedData)
137
{
138
139
ListItem item = new ListItem();
140
141
142
if (!String.IsNullOrEmpty(DataTextField))
143
{
144
item.Text = DataBinder.GetPropertyValue(dataItem,
145
DataTextField, null); //以后要加上DataFormat
146
147
item.Value = DataBinder.GetPropertyValue(dataItem,
148
DataValueField, null);
149
}
150
else
151
{
152
PropertyDescriptorCollection props =
153
TypeDescriptor.GetProperties(dataItem);
154
155
// Set the "default" value of the TextBox.
156
item.Text = "";
157
item.Value = "";
158
159
// Set the true data-bound value of the TextBox,
160
// if possible.
161
if (props.Count >= 1)
162
{
163
if (null != props[0].GetValue(dataItem))
164
{
165
item.Text = props[0].GetValue(dataItem).ToString();
166
item.Value = props[0].GetValue(dataItem).ToString();
167
}
168
}
169
}
170
171
ListSource.Add(item);
172
}
173
}
174
}
175
176
public override void RenderBeginTag(HtmlTextWriter writer)
177
{
178
writer.WriteBeginTag("div>");
179
}
180
181
protected override void RenderContents(HtmlTextWriter output)
182
{
183
// Make sure the control is declared in a form tag
184
// with runat=server.
185
if (Page != null)
186
{
187
Page.VerifyRenderingInServerForm(this);
188
}
189
190
191
//生成输入文本框的Html字符串,呈现在页面
192
String inputTextBox = prepareInputBox();
193
output.Write(inputTextBox);
194
195
output.Write("<BR>");
196
197
//生成备选框的Html字符串,呈现在页面
198
String listBox = prepareListBox();
199
output.Write(listBox);
200
201
}
202
203
204
public override void RenderEndTag(HtmlTextWriter writer)
205
{
206
writer.WriteEndTag("div");
207
}
208
209
Private
249
}
250
}
251

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

249

250

251

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述