在教科书上,通常举例只是简单的例子,定制控件包括子控件,也是只有一个子控件,这里我们实现一个有多个子控件的定制控件SMTextboxList,它包括三组数组成员,一个是Label值的数组,一个是TextBox值的数组,一个是标志TextBox是否Enable的数组.
  1
using System;
2
using System.Text;
3
using System.Web.UI;
4
using System.Web.UI.WebControls;
5
using System.ComponentModel;
6
using System.Drawing;
7
using System.Web.UI.Design;
8
using System.Collections;
9
using System.Data;
10
using System.Drawing.Design;
11
using Libra.Workflow.SmartformControls.TextBoxListConverter;
12![]()
13
[assembly:TagPrefix("Libra.Workflow.SmartformControls","SMControls")]
14
namespace Libra.Workflow.SmartformControls
15![]()
![]()
{
16![]()
    /**//// <summary>
17
    /// TextBoxList 的摘要说明。
18
    /// </summary>
19
    /// 
20
    [ToolboxBitmap(typeof(SMTextBoxList),"Libra.Workflow.SmartformControls.SMTextBoxList.bmp")]
21
    [ToolboxData("<{0}:SMTextBoxList runat=server>{0}</{0}:SMTextBoxList>")]    
22
    public class SMTextBoxList:Control,ISMControl,IPostBackDataHandler
23![]()
    
{        
24
        public SMTextBoxList()
25![]()
        
{
26
            //
27
            // TODO: 在此处添加构造函数逻辑
28
            //            
29
        }        
30![]()
31![]()
32![]()
        控件属性#region 控件属性
33![]()
34
        //此属性是在ToolBox中,根据此字段来提取一个string[]数组,
35
        //这个数组就是显示在Label上的值
36
        [Bindable(false),Category("SmartForm"),DefaultValue("")] 
37
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]
38
        public string NameField
39![]()
        
{            
40
            get
41![]()
            
{
42
                return (string)ViewState["_NameField"];
43
            }
44
            set
45![]()
            
{                
46
                ViewState["_NameField"] = value;
47
            }
48
        }
49
        
50
        //根据此字段提取一个string[]数组
51
        //这个数组显示在TextBox中的值
52
        [Bindable(false),Category("SmartForm"),DefaultValue("")] 
53
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]
54
        public string ValueField
55![]()
        
{
56
            get
57![]()
            
{
58
                return (string)ViewState["_ValueField"];
59
            }
60
            set
61![]()
            
{                
62
                ViewState["_ValueField"] = value;
63
            }
64
        }
65
        
66
        //根据此字段提取一个string[]数组,但是它是由true,false组成的
67
        //控制TextBox控件是否可用
68
        [Bindable(false),Category("SmartForm"),DefaultValue("")] 
69
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]
70
        public string EnableField
71![]()
        
{
72
            get
73![]()
            
{
74
                return (string)ViewState["_EnableField"];
75
            }
76
            set
77![]()
            
{                
78
                ViewState["_EnableField"] = value;
79
            }
80
        }
81
        
82
        //根据NameField返回的string[],用来显示在Label控件上
83
        private string[] LabelTextValue
84![]()
        
{
85
            get 
86![]()
            
{
87
                return (string[])ViewState["_LabelTextValue"];
88
            }
89
            set
90![]()
            
{
91
                ViewState["_LabelTextValue"] = value;
92
            }
93
        }
94
        //根据ValueField返回的string[],用来显示大在TextBox控件上
95
        private string[] TextBoxTextValue
96![]()
        
{
97
            get 
98![]()
            
{
99
                return (string[])ViewState["_TextBoxTextValue"];
100
            }
101
            set
102![]()
            
{
103
                ViewState["_TextBoxTextValue"] = value;
104
            }
105
        }
106![]()
107
        //根据EnableField返回的string[]数组,成员都是true,false,用来控制
108
        //对应的TextBox是否可用
109
        //另外,此数组还可以由另外一个参数EnableIndexField提取的整数值来
110
        //设定,比如根据EnableIndexField提取的值是3,则在我们这个父控件的
111
        //第4个子控件TextBox是不可用的.
112
        private string[] ControlEnabledValue
113![]()
        
{
114
            get
115![]()
            
{
116
                return (string[])ViewState["_ControlEnabledValue"];
117
            }
118
            set
119![]()
            
{
120
                ViewState["_ControlEnabledValue"] = value;
121
            }
122
        }
123![]()
124
        //数据源,用来使本控件支持DataBind()技术
125
        [Category("SmartForm"),DefaultValue(""),Browsable(false)] 
126
        private object DataSource
127![]()
        
{
128
            get 
129![]()
            
{ 
130
                return ViewState["_DataSource"]; 
131
            }
132
            set 
133![]()
            
{ 
134
                ViewState["_DataSource"] = value;
135
            }
136
        }
137![]()
138
        //可以根据这个属性提取位于哪个索引位置上的TextBox可用
139
        [Category("SmartForm"),Browsable(true)]
140
        public string EnableIndexField
141![]()
        
{
142
            get
143![]()
            
{
144
                return (string)ViewState["_EnableIndexField"];
145
            }
146
            set
147![]()
            
{
148
                ViewState["_EnableIndexField"] = value;
149
            }
150
        }
151![]()
152
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     
153
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]
154
        public string DataField
155![]()
        
{
156
            get
157![]()
            
{
158
                return (string)ViewState["_DataField"];
159
            }
160![]()
161
            set
162![]()
            
{                
163
                ViewState["_DataField"] = value;
164
            }
165
        }
166
        
167
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     
168
        public string DataValue
169![]()
        
{
170
            get
171![]()
            
{
172
                return (string)ViewState["_DataValue"];
173
            }
174
            set
175![]()
            
{
176
                ViewState["_DataValue"] = value;
177
            }
178
        }
179![]()
180
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     
181
        public string OriginalDataValue
182![]()
        
{
183
            get
184![]()
            
{
185
                return (string)ViewState["_OriginalDataValue"];
186
            }
187
            set
188![]()
            
{
189
                ViewState["_OriginalDataValue"] = value;
190
            }
191
        }
192![]()
193
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     
194
        public System.Type DataType
195![]()
        
{
196
            set
197![]()
            
{
198
                ViewState["_DataType"] = value;
199
            }
200
            get
201![]()
            
{
202
                return (System.Type)ViewState["_DataType"];
203
            }
204
        }
205
        //这是我们的一个私有数据成员,用来把控件的三个数据属性转换后存在这里
206
        private ArrayList _CacheData = new ArrayList();
207
        #endregion
208![]()
209![]()
        数据绑定部分DataBinding,当调用ISMControl的BindData()方法时,会调用此部分代码,从OnDataBinding()开始#region 数据绑定部分DataBinding,当调用ISMControl的BindData()方法时,会调用此部分代码,从OnDataBinding()开始
210
        private object[] GetDataItem(object item)
211![]()
        
{
212
            object[] result = null;
213
            if( item is DataRowView)
214![]()
            
{
215
                DataRowView drv = (DataRowView)item;
216
                result = new string[3];
217
                result[0] = drv[0].ToString();
218
                result[1] = drv[1].ToString();
219
                result[2] = drv[2].ToString();
220
            }            
221
            else
222
                result = null;
223
            return result;
224
        }
225![]()
226
        public IEnumerable GetResolvedDataSource(object ds)
227![]()
        
{
228
            if ( ds is IEnumerable )
229
                return (IEnumerable)ds;
230
            else if (ds is DataTable)
231
                return (IEnumerable)(((DataTable)ds).DefaultView);
232
            else if( ds is DataSet )
233![]()
            
{
234
                DataView dv = ((DataSet)ds).Tables[0].DefaultView;
235
                return (IEnumerable)dv;
236
            }
237
            else if( ds is IList )
238
                return (IEnumerable)((IList)ds);
239
            else
240
                return null;
241
        }
242![]()
243
        protected override void OnDataBinding(EventArgs e)
244![]()
        
{
245
            base.OnDataBinding (e);
246
            if( DataSource != null )
247![]()
            
{
248
                IEnumerable ds = this.GetResolvedDataSource(DataSource);
249
                IEnumerator dataEnum = ds.GetEnumerator();
250
                _CacheData.Clear();
251
                while(dataEnum.MoveNext())
252![]()
                
{                    
253
                    _CacheData.Add( this.GetDataItem(dataEnum.Current) );                    
254
                }
255
                ViewState["_CacheData"] = _CacheData;
256
            }
257
        }
258
        #endregion
259
    
260![]()
        控件显示部分#region 控件显示部分        
261
        protected override void Render(HtmlTextWriter output)
262![]()
        
{
263
            _CacheData = (ArrayList)ViewState["_CacheData"];
264
            if( this._CacheData == null )
265
                return;
266
            output.AddAttribute(HtmlTextWriterAttribute.Border,"0");
267
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
268
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
269
            output.RenderBeginTag(HtmlTextWriterTag.Table);
270
            for(int i=0; i < _CacheData.Count; i++)
271![]()
            
{
272
                string[] ddr = (string[])_CacheData[i];
273
                output.RenderBeginTag(HtmlTextWriterTag.Tr);                
274
                    output.RenderBeginTag(HtmlTextWriterTag.Td);        
275
                        output.Write(ddr[0]);
276
                    output.RenderEndTag();
277
                    output.RenderBeginTag(HtmlTextWriterTag.Td);
278
                        output.AddAttribute(HtmlTextWriterAttribute.Name,UniqueID);
279
                        output.AddAttribute(HtmlTextWriterAttribute.Value,ddr[1]);
280
                        if( ddr[2].ToUpper() == "FALSE" )
281
                            output.AddAttribute(HtmlTextWriterAttribute.Disabled,"true");
282
                        output.RenderBeginTag(HtmlTextWriterTag.Input);
283
                    output.RenderEndTag();
284
                output.RenderEndTag();            
285
            }
286
            output.RenderEndTag();
287
        }
288
        #endregion
289![]()
290![]()
        ViewState保存数据部分#region ViewState保存数据部分
291
        protected override void LoadViewState(object savedState)
292![]()
        
{
293
            if ( savedState != null )
294![]()
            
{
295
                object[] vState = (object[])savedState;
296
                if( vState[0] != null )
297
                    base.LoadViewState(vState[0]);
298
                if( vState[1] != null )
299
                    this.LabelTextValue = (string[])vState[1];
300
                if( vState[2] != null )
301
                    this.TextBoxTextValue = (string[])vState[2];
302
                if( vState[3] != null )
303
                    this.ControlEnabledValue = (string[])vState[3];
304
            }            
305
        }
306
        protected override object SaveViewState()
307![]()
        
{
308
            object[] vState = new object[4];
309
            vState[0] = base.SaveViewState();
310
            vState[1] = this.LabelTextValue;
311
            vState[2] = this.TextBoxTextValue;
312
            vState[3] = this.ControlEnabledValue;
313
            return vState;    
314
        }
315
        #endregion
316![]()
317![]()
        ISMControl 成员#region ISMControl 成员
318
        //调用顺序:排第一,从这里开始
319
        public void BindAttributeData(Libra.Workflow.Web.SmartForm ParentPage)
320![]()
        
{
321
            // TODO:  添加 SMTextBox.LoadAttribute 实现
322
            int max = 0;
323
            if( this.NameField != null && this.NameField != "" )
324![]()
            
{
325
                this.LabelTextValue = (string[])DataHelper.GetTokenDataItemValue(ParentPage,this.NameField);
326
                if( this.LabelTextValue != null)
327![]()
                
{
328
                    max = this.LabelTextValue.Length;
329
                }
330
            }
331![]()
332
            if( this.ValueField != null && this.ValueField != "" )
333![]()
            
{
334
                this.TextBoxTextValue = (string[])DataHelper.GetTokenDataItemValue(ParentPage,this.ValueField);
335
                if( this.TextBoxTextValue != null )
336![]()
                
{
337
                    int i = TextBoxTextValue.Length;
338
                    if( max < i )
339
                        max = i;
340
                }
341
            }
342![]()
343
            if( this.EnableField != null && this.EnableField != "" )
344![]()
            
{
345
                this.ControlEnabledValue = (string[])DataHelper.GetTokenDataItemValue(ParentPage,this.EnableField);            
346
                if( this.ControlEnabledValue != null )
347![]()
                
{
348
                    int i = this.ControlEnabledValue.Length;
349
                    if( max < i )
350
                        max = i;
351
                }
352
            }
353![]()
354
            if( this.EnableIndexField != null && this.EnableIndexField != "")
355![]()
            
{
356
                object IndexObject = (int)DataHelper.GetTokenDataItemValue(ParentPage,EnableIndexField);
357
                int index = -1;
358
                if( IndexObject != null )
359
                    index = (int)IndexObject;
360
                StringBuilder strBuilder = new StringBuilder();
361
                for(int count = 0; count < max ; count++ )
362![]()
                
{
363
                    if( count == index )
364
                        strBuilder.Append("true");
365
                    else
366
                        strBuilder.Append("false");
367
                    strBuilder.Append("&");
368
                }
369
                
370
                strBuilder = strBuilder.Remove(strBuilder.Length-1,1);
371![]()
372
                this.ControlEnabledValue = (strBuilder.ToString()).Split('&');
373![]()
374
            }
375
            
376![]()
377
            if( this.LabelTextValue != null || this.TextBoxTextValue != null || this.ControlEnabledValue != null )
378![]()
            
{
379
                ArrayList al = new ArrayList();                
380
                al.Add(this.LabelTextValue);
381
                al.Add(this.TextBoxTextValue);
382
                al.Add(this.ControlEnabledValue);
383
                this.DataSource = al;
384
                DataTable dt = new ArrayConvertToDataTable().ConvertToDataTable(this.DataSource);
385
                this.DataSource = dt;
386
            }
387
        }
388![]()
389![]()
390
        //调用顺序:排第二,绑定数据,调用OnDataBinding方法
391
        public void BindData(Libra.Workflow.Web.SmartForm ParentPage)
392![]()
        
{
393
            // TODO:  添加 SMTextBoxList.BindData 实现
394
            this.DataBind();
395
        }
396![]()
397![]()
398
        //调用顺序的第三步:提交数据部分
399
        public void SubmitData(Libra.Workflow.Web.SmartForm ParentPage)
400![]()
        
{
401
            // TODO:  添加 SMTextBoxList.SubmitData 实现
402
            if( this.ValueField != null )
403![]()
            
{
404
                for( int i = 0; i < this.ValueField.Length; i++)
405![]()
                
{                    
406
                    DataHelper.SaveTokenDataItemValue(ParentPage,ValueField,TextBoxTextValue);
407
                }
408
            }
409
        }        
410
        #endregion
411![]()
412![]()
        IPostBackDataHandler 成员#region IPostBackDataHandler 成员
413![]()
414
        public void RaisePostDataChangedEvent()
415![]()
        
{
416
            // TODO:  添加 SMTextBoxList.RaisePostDataChangedEvent 实现
417
            //重新组装数据,设置_CacheData
418
            ArrayList al = new ArrayList();        
419
            al.Add(this.LabelTextValue);
420
            al.Add(this.TextBoxTextValue);
421
            al.Add(this.ControlEnabledValue);
422
            _CacheData = al;
423
            this.DataSource = _CacheData;
424
            
425
            //ArrayConvertToDataTable意义如名字所示,这里没有列出代码
426
            DataTable dt = new ArrayConvertToDataTable().ConvertToDataTable(this.DataSource);
427
            //指定下面的意义在于,让控件与变化后的数据源绑定,而不是旧的数据源,这样保证了更新
428
            this.DataSource = dt;            
429
            this.DataBind();
430
        }
431![]()
432
        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
433![]()
        
{
434
            // TODO:  添加 SMTextBoxList.LoadPostData 实现            
435
            if(postDataKey != this.ClientID)
436
                return false;
437
            int index = 0;
438
            for(index = 0; index < postCollection.Count; index++)
439![]()
            
{
440
                //如果ClientID是我们这个控件,则Break,执行下面的代码
441
                if(this.ClientID == postCollection.GetKey(index))
442
                    break;
443
            }
444
            if(index >= postCollection.Count)
445
                return false;
446
            //记录PostBack之前的值
447
            string[] presentValue = this.TextBoxTextValue;
448
            //记录PostBack回来的值,也就是从客户端发来的值
449
            string[] postedValue = postCollection.GetValues(index);
450
            if( presentValue == null || !presentValue.Equals(postedValue) )
451![]()
            
{
452
                int j = 0;
453
                if( this.ControlEnabledValue != null)
454![]()
                
{
455
                    for( int i=0; i<this.ControlEnabledValue.Length; i++)
456![]()
                    
{
457
                        if( this.ControlEnabledValue[i].ToUpper() == "TRUE" )
458![]()
                        
{
459
                            presentValue[i] = postedValue[j];
460
                            j++;
461
                        }
462
                    }
463
                    this.TextBoxTextValue = presentValue;                
464
                }
465
                else
466
                    this.TextBoxTextValue = postedValue;
467
                return true;
468
            }            
469
            return false;             
470
        }
471![]()
472
        #endregion
473
    }
474
}
475![]()
using System;2
using System.Text;3
using System.Web.UI;4
using System.Web.UI.WebControls;5
using System.ComponentModel;6
using System.Drawing;7
using System.Web.UI.Design;8
using System.Collections;9
using System.Data;10
using System.Drawing.Design;11
using Libra.Workflow.SmartformControls.TextBoxListConverter;12

13
[assembly:TagPrefix("Libra.Workflow.SmartformControls","SMControls")]14
namespace Libra.Workflow.SmartformControls15


{16

    /**//// <summary>17
    /// TextBoxList 的摘要说明。18
    /// </summary>19
    /// 20
    [ToolboxBitmap(typeof(SMTextBoxList),"Libra.Workflow.SmartformControls.SMTextBoxList.bmp")]21
    [ToolboxData("<{0}:SMTextBoxList runat=server>{0}</{0}:SMTextBoxList>")]    22
    public class SMTextBoxList:Control,ISMControl,IPostBackDataHandler23

    
{        24
        public SMTextBoxList()25

        
{26
            //27
            // TODO: 在此处添加构造函数逻辑28
            //            29
        }        30

31

32

        控件属性#region 控件属性33

34
        //此属性是在ToolBox中,根据此字段来提取一个string[]数组,35
        //这个数组就是显示在Label上的值36
        [Bindable(false),Category("SmartForm"),DefaultValue("")] 37
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]38
        public string NameField39

        
{            40
            get41

            
{42
                return (string)ViewState["_NameField"];43
            }44
            set45

            
{                46
                ViewState["_NameField"] = value;47
            }48
        }49
        50
        //根据此字段提取一个string[]数组51
        //这个数组显示在TextBox中的值52
        [Bindable(false),Category("SmartForm"),DefaultValue("")] 53
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]54
        public string ValueField55

        
{56
            get57

            
{58
                return (string)ViewState["_ValueField"];59
            }60
            set61

            
{                62
                ViewState["_ValueField"] = value;63
            }64
        }65
        66
        //根据此字段提取一个string[]数组,但是它是由true,false组成的67
        //控制TextBox控件是否可用68
        [Bindable(false),Category("SmartForm"),DefaultValue("")] 69
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]70
        public string EnableField71

        
{72
            get73

            
{74
                return (string)ViewState["_EnableField"];75
            }76
            set77

            
{                78
                ViewState["_EnableField"] = value;79
            }80
        }81
        82
        //根据NameField返回的string[],用来显示在Label控件上83
        private string[] LabelTextValue84

        
{85
            get 86

            
{87
                return (string[])ViewState["_LabelTextValue"];88
            }89
            set90

            
{91
                ViewState["_LabelTextValue"] = value;92
            }93
        }94
        //根据ValueField返回的string[],用来显示大在TextBox控件上95
        private string[] TextBoxTextValue96

        
{97
            get 98

            
{99
                return (string[])ViewState["_TextBoxTextValue"];100
            }101
            set102

            
{103
                ViewState["_TextBoxTextValue"] = value;104
            }105
        }106

107
        //根据EnableField返回的string[]数组,成员都是true,false,用来控制108
        //对应的TextBox是否可用109
        //另外,此数组还可以由另外一个参数EnableIndexField提取的整数值来110
        //设定,比如根据EnableIndexField提取的值是3,则在我们这个父控件的111
        //第4个子控件TextBox是不可用的.112
        private string[] ControlEnabledValue113

        
{114
            get115

            
{116
                return (string[])ViewState["_ControlEnabledValue"];117
            }118
            set119

            
{120
                ViewState["_ControlEnabledValue"] = value;121
            }122
        }123

124
        //数据源,用来使本控件支持DataBind()技术125
        [Category("SmartForm"),DefaultValue(""),Browsable(false)] 126
        private object DataSource127

        
{128
            get 129

            
{ 130
                return ViewState["_DataSource"]; 131
            }132
            set 133

            
{ 134
                ViewState["_DataSource"] = value;135
            }136
        }137

138
        //可以根据这个属性提取位于哪个索引位置上的TextBox可用139
        [Category("SmartForm"),Browsable(true)]140
        public string EnableIndexField141

        
{142
            get143

            
{144
                return (string)ViewState["_EnableIndexField"];145
            }146
            set147

            
{148
                ViewState["_EnableIndexField"] = value;149
            }150
        }151

152
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     153
        [Editor(typeof(DataSelector),typeof(UITypeEditor))]154
        public string DataField155

        
{156
            get157

            
{158
                return (string)ViewState["_DataField"];159
            }160

161
            set162

            
{                163
                ViewState["_DataField"] = value;164
            }165
        }166
        167
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     168
        public string DataValue169

        
{170
            get171

            
{172
                return (string)ViewState["_DataValue"];173
            }174
            set175

            
{176
                ViewState["_DataValue"] = value;177
            }178
        }179

180
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     181
        public string OriginalDataValue182

        
{183
            get184

            
{185
                return (string)ViewState["_OriginalDataValue"];186
            }187
            set188

            
{189
                ViewState["_OriginalDataValue"] = value;190
            }191
        }192

193
        [Category("SmartForm"),DefaultValue(""),Browsable(false)]     194
        public System.Type DataType195

        
{196
            set197

            
{198
                ViewState["_DataType"] = value;199
            }200
            get201

            
{202
                return (System.Type)ViewState["_DataType"];203
            }204
        }205
        //这是我们的一个私有数据成员,用来把控件的三个数据属性转换后存在这里206
        private ArrayList _CacheData = new ArrayList();207
        #endregion208

209

        数据绑定部分DataBinding,当调用ISMControl的BindData()方法时,会调用此部分代码,从OnDataBinding()开始#region 数据绑定部分DataBinding,当调用ISMControl的BindData()方法时,会调用此部分代码,从OnDataBinding()开始210
        private object[] GetDataItem(object item)211

        
{212
            object[] result = null;213
            if( item is DataRowView)214

            
{215
                DataRowView drv = (DataRowView)item;216
                result = new string[3];217
                result[0] = drv[0].ToString();218
                result[1] = drv[1].ToString();219
                result[2] = drv[2].ToString();220
            }            221
            else222
                result = null;223
            return result;224
        }225

226
        public IEnumerable GetResolvedDataSource(object ds)227

        
{228
            if ( ds is IEnumerable )229
                return (IEnumerable)ds;230
            else if (ds is DataTable)231
                return (IEnumerable)(((DataTable)ds).DefaultView);232
            else if( ds is DataSet )233

            
{234
                DataView dv = ((DataSet)ds).Tables[0].DefaultView;235
                return (IEnumerable)dv;236
            }237
            else if( ds is IList )238
                return (IEnumerable)((IList)ds);239
            else240
                return null;241
        }242

243
        protected override void OnDataBinding(EventArgs e)244

        
{245
            base.OnDataBinding (e);246
            if( DataSource != null )247

            
{248
                IEnumerable ds = this.GetResolvedDataSource(DataSource);249
                IEnumerator dataEnum = ds.GetEnumerator();250
                _CacheData.Clear();251
                while(dataEnum.MoveNext())252

                
{                    253
                    _CacheData.Add( this.GetDataItem(dataEnum.Current) );                    254
                }255
                ViewState["_CacheData"] = _CacheData;256
            }257
        }258
        #endregion259
    260

        控件显示部分#region 控件显示部分        261
        protected override void Render(HtmlTextWriter output)262

        
{263
            _CacheData = (ArrayList)ViewState["_CacheData"];264
            if( this._CacheData == null )265
                return;266
            output.AddAttribute(HtmlTextWriterAttribute.Border,"0");267
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");268
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");269
            output.RenderBeginTag(HtmlTextWriterTag.Table);270
            for(int i=0; i < _CacheData.Count; i++)271

            
{272
                string[] ddr = (string[])_CacheData[i];273
                output.RenderBeginTag(HtmlTextWriterTag.Tr);                274
                    output.RenderBeginTag(HtmlTextWriterTag.Td);        275
                        output.Write(ddr[0]);276
                    output.RenderEndTag();277
                    output.RenderBeginTag(HtmlTextWriterTag.Td);278
                        output.AddAttribute(HtmlTextWriterAttribute.Name,UniqueID);279
                        output.AddAttribute(HtmlTextWriterAttribute.Value,ddr[1]);280
                        if( ddr[2].ToUpper() == "FALSE" )281
                            output.AddAttribute(HtmlTextWriterAttribute.Disabled,"true");282
                        output.RenderBeginTag(HtmlTextWriterTag.Input);283
                    output.RenderEndTag();284
                output.RenderEndTag();            285
            }286
            output.RenderEndTag();287
        }288
        #endregion289

290

        ViewState保存数据部分#region ViewState保存数据部分291
        protected override void LoadViewState(object savedState)292

        
{293
            if ( savedState != null )294

            
{295
                object[] vState = (object[])savedState;296
                if( vState[0] != null )297
                    base.LoadViewState(vState[0]);298
                if( vState[1] != null )299
                    this.LabelTextValue = (string[])vState[1];300
                if( vState[2] != null )301
                    this.TextBoxTextValue = (string[])vState[2];302
                if( vState[3] != null )303
                    this.ControlEnabledValue = (string[])vState[3];304
            }            305
        }306
        protected override object SaveViewState()307

        
{308
            object[] vState = new object[4];309
            vState[0] = base.SaveViewState();310
            vState[1] = this.LabelTextValue;311
            vState[2] = this.TextBoxTextValue;312
            vState[3] = this.ControlEnabledValue;313
            return vState;    314
        }315
        #endregion316

317

        ISMControl 成员#region ISMControl 成员318
        //调用顺序:排第一,从这里开始319
        public void BindAttributeData(Libra.Workflow.Web.SmartForm ParentPage)320

        
{321
            // TODO:  添加 SMTextBox.LoadAttribute 实现322
            int max = 0;323
            if( this.NameField != null && this.NameField != "" )324

            
{325
                this.LabelTextValue = (string[])DataHelper.GetTokenDataItemValue(ParentPage,this.NameField);326
                if( this.LabelTextValue != null)327

                
{328
                    max = this.LabelTextValue.Length;329
                }330
            }331

332
            if( this.ValueField != null && this.ValueField != "" )333

            
{334
                this.TextBoxTextValue = (string[])DataHelper.GetTokenDataItemValue(ParentPage,this.ValueField);335
                if( this.TextBoxTextValue != null )336

                
{337
                    int i = TextBoxTextValue.Length;338
                    if( max < i )339
                        max = i;340
                }341
            }342

343
            if( this.EnableField != null && this.EnableField != "" )344

            
{345
                this.ControlEnabledValue = (string[])DataHelper.GetTokenDataItemValue(ParentPage,this.EnableField);            346
                if( this.ControlEnabledValue != null )347

                
{348
                    int i = this.ControlEnabledValue.Length;349
                    if( max < i )350
                        max = i;351
                }352
            }353

354
            if( this.EnableIndexField != null && this.EnableIndexField != "")355

            
{356
                object IndexObject = (int)DataHelper.GetTokenDataItemValue(ParentPage,EnableIndexField);357
                int index = -1;358
                if( IndexObject != null )359
                    index = (int)IndexObject;360
                StringBuilder strBuilder = new StringBuilder();361
                for(int count = 0; count < max ; count++ )362

                
{363
                    if( count == index )364
                        strBuilder.Append("true");365
                    else366
                        strBuilder.Append("false");367
                    strBuilder.Append("&");368
                }369
                370
                strBuilder = strBuilder.Remove(strBuilder.Length-1,1);371

372
                this.ControlEnabledValue = (strBuilder.ToString()).Split('&');373

374
            }375
            376

377
            if( this.LabelTextValue != null || this.TextBoxTextValue != null || this.ControlEnabledValue != null )378

            
{379
                ArrayList al = new ArrayList();                380
                al.Add(this.LabelTextValue);381
                al.Add(this.TextBoxTextValue);382
                al.Add(this.ControlEnabledValue);383
                this.DataSource = al;384
                DataTable dt = new ArrayConvertToDataTable().ConvertToDataTable(this.DataSource);385
                this.DataSource = dt;386
            }387
        }388

389

390
        //调用顺序:排第二,绑定数据,调用OnDataBinding方法391
        public void BindData(Libra.Workflow.Web.SmartForm ParentPage)392

        
{393
            // TODO:  添加 SMTextBoxList.BindData 实现394
            this.DataBind();395
        }396

397

398
        //调用顺序的第三步:提交数据部分399
        public void SubmitData(Libra.Workflow.Web.SmartForm ParentPage)400

        
{401
            // TODO:  添加 SMTextBoxList.SubmitData 实现402
            if( this.ValueField != null )403

            
{404
                for( int i = 0; i < this.ValueField.Length; i++)405

                
{                    406
                    DataHelper.SaveTokenDataItemValue(ParentPage,ValueField,TextBoxTextValue);407
                }408
            }409
        }        410
        #endregion411

412

        IPostBackDataHandler 成员#region IPostBackDataHandler 成员413

414
        public void RaisePostDataChangedEvent()415

        
{416
            // TODO:  添加 SMTextBoxList.RaisePostDataChangedEvent 实现417
            //重新组装数据,设置_CacheData418
            ArrayList al = new ArrayList();        419
            al.Add(this.LabelTextValue);420
            al.Add(this.TextBoxTextValue);421
            al.Add(this.ControlEnabledValue);422
            _CacheData = al;423
            this.DataSource = _CacheData;424
            425
            //ArrayConvertToDataTable意义如名字所示,这里没有列出代码426
            DataTable dt = new ArrayConvertToDataTable().ConvertToDataTable(this.DataSource);427
            //指定下面的意义在于,让控件与变化后的数据源绑定,而不是旧的数据源,这样保证了更新428
            this.DataSource = dt;            429
            this.DataBind();430
        }431

432
        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)433

        
{434
            // TODO:  添加 SMTextBoxList.LoadPostData 实现            435
            if(postDataKey != this.ClientID)436
                return false;437
            int index = 0;438
            for(index = 0; index < postCollection.Count; index++)439

            
{440
                //如果ClientID是我们这个控件,则Break,执行下面的代码441
                if(this.ClientID == postCollection.GetKey(index))442
                    break;443
            }444
            if(index >= postCollection.Count)445
                return false;446
            //记录PostBack之前的值447
            string[] presentValue = this.TextBoxTextValue;448
            //记录PostBack回来的值,也就是从客户端发来的值449
            string[] postedValue = postCollection.GetValues(index);450
            if( presentValue == null || !presentValue.Equals(postedValue) )451

            
{452
                int j = 0;453
                if( this.ControlEnabledValue != null)454

                
{455
                    for( int i=0; i<this.ControlEnabledValue.Length; i++)456

                    
{457
                        if( this.ControlEnabledValue[i].ToUpper() == "TRUE" )458

                        
{459
                            presentValue[i] = postedValue[j];460
                            j++;461
                        }462
                    }463
                    this.TextBoxTextValue = presentValue;                464
                }465
                else466
                    this.TextBoxTextValue = postedValue;467
                return true;468
            }            469
            return false;             470
        }471

472
        #endregion473
    }474
}475

                    
                
                
                
            
        
浙公网安备 33010602011771号