首页 > 其他 > 详细

cyq.data自动绑定MActionUI.cs修改

时间:2014-03-15 20:16:31      阅读:719      评论:0      收藏:0      [点我收藏+]

cyq.data的自动赋值取值都是依据控件前三位后边的为字段依据,修改了一下。改成如果不使用SetAutoPrefix方法,则自动获取和字段一一对应的数据,使用SetAutoPrefix方法后才使用指定的前缀。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected void Page_Load(object sender, EventArgs e)
{
    UserName.Text = "";
    UserPwd.Text = "";
    RealName.Text = "";
    Email.Text = "";
    Mobile.Text = "";
    MAction action = new MAction("Base_Admin");
    action.Fill("id=1");
    action.SetToAll(this);
    action.Close();
}
 
protected void Button1_Click(object sender, EventArgs e)
{
    MAction action = new MAction("Base_Admin");
    //action.Set("username", "123123");
    action.Insert(true);
    action.Close();
}

  上边这种是把数据直接绑定到页面,控件ID和数据库字段一一对应。再新增插入的时候也无须指定控件前缀,默认取数据库字段名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
protected void Page_Load(object sender, EventArgs e)
{
    UserName.Text = "";
    UserPwd.Text = "";
    RealName.Text = "";
    Email.Text = "";
    Mobile.Text = "";
    MAction action = new MAction("Base_Admin");
    action.Fill("id=1");
        action.SetAutoPrefix("txt","ddl");
    action.SetToAll(this);
    action.Close();
}
 
protected void Button1_Click(object sender, EventArgs e)
{
    MAction action = new MAction("Base_Admin");
    //action.Set("username", "123123");
        action.SetAutoPrefix("txt","ddl");
    action.Insert(true);
    action.Close();
}   

 这种是指定了控件前缀,把所有已txt、ddl开头连上数据库字段的控件绑定,插入的时候插入txt、ddl开头的和数据库字段对应的数据。

这样如果字段和控件直接对应的情况,就不用考虑SetAutoPrefix方法了。用了SetAutoPrefix方法,则和以前的cyq一样使用。

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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Win = System.Windows.Forms;
using CYQ.Data.Table;
using System.Collections.Generic;
using CYQ.Data.SQL;
using System.Data;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
namespace CYQ.Data
{
    internal class MActionUI : IDisposable
    {
        private List<string> autoPrefixList = new List<string>() { "" };//调用插入和更新,自动获取控件名的前缀
        public MDataRow _Row;
        public MActionUI(ref MDataRow row)
        {
            _Row = row;
        }
 
        #region UI操作分路
        public void Set(object ct, object value, bool isControlEnabled)
        {
            if (ct is Control)
            {
                SetTo(ct as Control, value, isControlEnabled);
            }
            else
            {
                SetTo(ct as Win.Control, value, isControlEnabled);
            }
        }
        public void Get(object ct, object value)
        {
            if (ct is Control)
            {
                GetFrom(ct as Control, value);
            }
            else
            {
                GetFrom(ct as Win.Control, value);
            }
        }
        #endregion
 
        #region WebUI操作
        public void SetTo(Control ct, object value, bool isControlEnabled)
        {
            string propName = string.Empty;
            if (string.IsNullOrEmpty(autoPrefixList[0]))
            {
                propName = ct.ID;
            }
            else
            {
                propName = ct.ID.Substring(3);
            }
            if (value == null)
            {
                value = _Row[propName].Value;
            }
            switch (ct.GetType().Name)
            {
                case "HtmlInputText":
                    ((HtmlInputText)ct).Value = Convert.ToString(value);
                    ((HtmlInputText)ct).Disabled = !isControlEnabled;
                    break;
                case "HtmlSelect":
                    ((HtmlSelect)ct).Value = Convert.ToString(value);
                    ((HtmlSelect)ct).Disabled = !isControlEnabled;
                    break;
                case "HtmlInputHidden":
                    ((HtmlInputHidden)ct).Value = Convert.ToString(value);
                    break;
                case "HtmlInputPassword":
                    ((HtmlInputPassword)ct).Value = Convert.ToString(value);
                    ((HtmlInputPassword)ct).Disabled = !isControlEnabled;
                    break;
                case "HtmlTextArea":
                    ((HtmlTextArea)ct).Value = Convert.ToString(value);
                    ((HtmlTextArea)ct).Disabled = !isControlEnabled;
                    break;
                case "Literal":
                    ((Literal)ct).Text = Convert.ToString(value);
                    break;
                case "Label":
                    ((Label)ct).Text = Convert.ToString(value);
                    break;
                case "HiddenField":
                    ((HiddenField)ct).Value = Convert.ToString(value);
                    break;
                case "TextBox":
                    ((TextBox)ct).Text = Convert.ToString(value);
                    ((TextBox)ct).Enabled = isControlEnabled;
                    break;
                case "DropDownList":
                    ((DropDownList)ct).SelectedValue = Convert.ToString(value);
                    ((DropDownList)ct).Enabled = isControlEnabled;
                    break;
                case "CheckBox":
                    bool tempValue;
                    if (Convert.ToString(value) == "1")
                    {
                        tempValue = true;
                    }
                    else
                    {
                        bool.TryParse(Convert.ToString(value), out tempValue);
                    }
                    ((CheckBox)ct).Checked = tempValue;
                    ((CheckBox)ct).Enabled = isControlEnabled;
                    break;
            }
        }
        /// <summary>
        /// 批量设置值
        /// </summary>
        public void SetToAll(object page, bool isControlEnabled)
        {
            MDataColumn mdc = _Row.Columns;
            for (int i = 0; i < mdc.Count; i++)
            {
                string key = mdc[i].ColumnName; //获取到列名
                string value = _Row[key].Value.ToString(); //对应值
                if (page is Control)
                {
                    foreach (string autoPrefix in autoPrefixList)
                    {
                        Control control = ((Control)page).FindControl(autoPrefix + key);
                        if (control != null)
                        {
                            SetTo(control, value, isControlEnabled);
                        }
                    }
                }
                else
                {
                    foreach (string autoPrefix in autoPrefixList)
                    {
                        Win.Control control = this.findControl((Win.Control)page, autoPrefix + key);
                        if (control != null)
                        {
                            SetTo(control, value, isControlEnabled);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 获取值
        /// </summary>
        public void GetFrom(Control ct, object value)
        {
            string propName = string.Empty;
            if (string.IsNullOrEmpty(autoPrefixList[0]))
            {
                propName = ct.ID;
            }
            else
            {
                propName = ct.ID.Substring(3);
            }
            if (value == null)
            {
                switch (ct.GetType().Name)
                {
                    case "HtmlInputText":
                        value = ((HtmlInputText)ct).Value;
                        break;
                    case "HtmlSelect":
                        value = ((HtmlSelect)ct).Value;
                        break;
                    case "HtmlInputHidden":
                        value = ((HtmlInputHidden)ct).Value;
                        break;
                    case "HtmlInputPassword":
                        value = ((HtmlInputPassword)ct).Value;
                        break;
                    case "HtmlTextArea":
                        value = ((HtmlTextArea)ct).Value;
                        break;
                    case "Literal":
                        value = ((Literal)ct).Text;
                        break;
                    case "Label":
                        value = ((Label)ct).Text;
                        break;
                    case "HiddenField":
                        value = ((HiddenField)ct).Value;
                        break;
                    case "TextBox":
                        value = ((TextBox)ct).Text.Trim();
                        break;
                    case "DropDownList":
                        value = ((DropDownList)ct).SelectedValue;
                        break;
                    case "CheckBox":
                        value = ((CheckBox)ct).Checked;
                        break;
                }
            }
            _Row[propName].Value = value;
        }
        #endregion
 
        #region WinUI操作
        public void SetTo(Win.Control ct, object value, bool isControlEnabled)
        {
            string propName = string.Empty;
            if (string.IsNullOrEmpty(autoPrefixList[0]))
            {
                propName = ct.Name;
            }
            else
            {
                propName = ct.Name.Substring(3);
            }
            if (value == null)
            {
                value = _Row[propName].Value;
            }
            switch (ct.GetType().Name)
            {
                case "TextBox":
                    ((Win.TextBox)ct).Text = Convert.ToString(value);
                    ((Win.TextBox)ct).Enabled = isControlEnabled;
                    break;
                case "ComboBox":
                    ((Win.ComboBox)ct).Items.Add(value);
                    break;
                case "Label":
                    ((Win.Label)ct).Text = Convert.ToString(value);
                    break;
                case "DateTimePicker":
                    DateTime dt;
                    if (DateTime.TryParse(Convert.ToString(value), out dt))
                    {
                        ((Win.DateTimePicker)ct).Value = dt;
                    }
                    break;
                case "ListBox":
                    ((Win.ListBox)ct).Items.Add(value);
                    break;
                case "CheckBox":
                    bool tempValue;
                    if (Convert.ToString(value) == "1")
                    {
                        tempValue = true;
                    }
                    else
                    {
                        bool.TryParse(Convert.ToString(value), out tempValue);
                    }
                    ((Win.CheckBox)ct).Checked = tempValue;
                    ((Win.CheckBox)ct).Enabled = isControlEnabled;
                    break;
                case "NumericUpDown":
                    decimal result = 0;
                    if (decimal.TryParse(Convert.ToString(value), out result))
                    {
                        ((Win.NumericUpDown)ct).Value = result;
                    }
                    break;
                case "RichTextBox":
                    ((Win.ListBox)ct).Text = Convert.ToString(value);
                    break;
            }
 
        }
 
        public void GetFrom(Win.Control ct, object value)
        {
            string propName = string.Empty;
            if (string.IsNullOrEmpty(autoPrefixList[0]))
            {
                propName = ct.Name;
            }
            else
            {
                propName = ct.Name.Substring(3);
            }
            if (value == null)
            {
                switch (ct.GetType().Name)
                {
                    case "TextBox":
                        value = ((Win.TextBox)ct).Text.Trim();
                        break;
                    case "ComboBox":
                        value = ((Win.ComboBox)ct).Text;
                        break;
                    case "Label":
                        value = ((Win.Label)ct).Text;
                        break;
                    case "DateTimePicker":
                        value = ((Win.DateTimePicker)ct).Value;
                        break;
                    case "ListBox":
                        value = ((Win.ListBox)ct).Text;
                        break;
                    case "CheckBox":
                        value = ((Win.CheckBox)ct).Checked;
                        break;
                    case "NumericUpDown":
                        value = ((Win.NumericUpDown)ct).Value;
                        break;
                    case "RichTextBox":
                        value = ((Win.RichTextBox)ct).Text;
                        break;
                }
            }
            _Row[propName].Value = value;
        }
        #endregion
 
        #region 自动取值
        /// <summary>
        /// 自动设置列的值(true为插入,false为更新)
        /// </summary>
        public void AutoSetColumnValue(bool containsID)
        {
            // Type type = null;
            int i = 0;
            if (containsID || !_Row[0]._CellValue.IsNull)
            {
                i = 1;
            }
            for (; i < _Row.Count; i++)
            {
                if (!_Row[i]._CellValue.IsChange)
                {
                    try
                    {
                        foreach (string autoPrefix in autoPrefixList)
                        {
                            if (System.Web.HttpContext.Current == null) //win批量取值
                            {
                                //待实现
                            }
                            else
                            {
                                string RequestValue = System.Web.HttpContext.Current.Request[autoPrefix + _Row[i]._CellStruct.ColumnName];
                                if (RequestValue != null)
                                {
                                    if (RequestValue == "on")
                                    {
                                        if (_Row[i]._CellStruct.SqlType == SqlDbType.Bit)
                                        {
                                            _Row[i].Value = true;
                                        }
                                        else
                                        {
                                            _Row[i].Value = 1;
                                        }
                                        break;
                                    }
                                    if (RequestValue.Length == 0 && DataType.GetGroupID(_Row[i]._CellStruct.SqlType) == 1)
                                    {
                                        _Row[i].Value = 0;
                                        break;
                                    }
                                    else if (_Row[i]._CellStruct.SqlType == SqlDbType.Bit && RequestValue.Length == 1)
                                    {
                                        _Row[i].Value = RequestValue == "1";
                                    }
                                    _Row[i].Value = TypeDescriptor.GetConverter(_Row[i]._CellStruct.ValueType).ConvertFrom(RequestValue.Trim());
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
        }
        #endregion
 
        #region 其它方法
        public void SetAutoPrefix(string autoPrefix, params string[] otherPrefix)
        {
            //autoPrefixList = new List<string>();
            autoPrefixList.Clear(); //清空默认值
            autoPrefixList.Add(autoPrefix);
            foreach (string item in otherPrefix)
            {
                autoPrefixList.Add(item);
            }
        }
        #endregion
 
        #region IDisposable 成员
 
        public void Dispose()
        {
            if (autoPrefixList != null)
            {
                autoPrefixList.Clear();
                autoPrefixList = null;
            }
        }
 
        #endregion
 
        #region 新增查找控件
        private Win.Control findControl(Win.Control control, string controlName)
        {
            Win.Control c1;
            foreach (Win.Control c in control.Controls)
            {
                if (c.Name == controlName)
                {
                    return c;
                }
                else if (c.Controls.Count > 0)
                {
                    c1 = findControl(c, controlName);
                    if (c1 != null)
                    {
                        return c1;
                    }
                }
            }
            return null;
        }
        #endregion
    }
}

 里边winform的批量赋值也已经一并修改好啦!

cyq.data自动绑定MActionUI.cs修改,布布扣,bubuko.com

cyq.data自动绑定MActionUI.cs修改

原文:http://www.cnblogs.com/homexigua/p/3602325.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!