1: public void BindToUI(Page UI, Type type, ArrayList ExcludeFromBind)
2: {
3: Type t = this.GetType();
4: foreach (FieldInfo c in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
5: {
6: if (ExcludeFromBind.Contains(c.Name))
7: {
8: continue;
9: }
10: if (c == null)
11: {
12: continue;
13: }
14: if (c.Name == null)
15: {
16: continue;
17: }
18:
19: //Retira o prefixo do nome do componente -> ele DEVE ter 3 letras, e o ID do controle deve ter mais que 3 letras
20: string PropertyPattern = (c.Name.Length < 3) ? "" : c.Name.Substring(3, c.Name.Length - 3);
21: PropertyPattern = PropertyPattern.Replace(t.Name, "");
22: if (PropertyPattern.Contains("_"))
23: {
24: PropertyPattern = PropertyPattern.Substring(0, PropertyPattern.IndexOf("_"));
25: }
26:
27: PropertyInfo pi = t.GetProperty(PropertyPattern);
28: if (pi != null)
29: {
30: if (pi.GetValue(this, null) == null)
31: {
32: continue;
33: }
34:
35: int IndexProp = -1;
36: HasIndexIdentifier(c.Name, out IndexProp);
37:
38: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.TextBox"))
39: {
40: int idx = 0;
41: string formatinfo = "";
42: if (ContainFormatInfo(pi.Name, out idx, out formatinfo))
43: {
44: if (!pi.PropertyType.IsArray)
45: {
46: (c.GetValue(UI) as TextBox).Text = String.Format(formatinfo, pi.GetValue(this, null));
47: }
48: else
49: {
50: object[] arrVal = (object[])pi.GetValue(this, null);
51: (c.GetValue(UI) as TextBox).Text = String.Format(formatinfo, arrVal[IndexProp]);
52: }
53: }
54: else
55: {
56: if (!pi.PropertyType.IsArray)
57: {
58: (c.GetValue(UI) as TextBox).Text = pi.GetValue(this, null).ToString();
59: }
60: else
61: {
62: object[] arrVal = (object[])pi.GetValue(this, null);
63: (c.GetValue(UI) as TextBox).Text = arrVal[IndexProp].ToString();
64: }
65: }
66: }
67: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.DropDownList"))
68: {
69: if (!pi.PropertyType.IsArray)
70: {
71: (c.GetValue(UI) as DropDownList).SelectedValue = pi.GetValue(this, null).ToString(); ;
72: }
73: else
74: {
75: object[] arrVal = (object[])pi.GetValue(this, null);
76: (c.GetValue(UI) as DropDownList).SelectedValue = arrVal[IndexProp].ToString();
77: }
78: }
79:
80:
81: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.RadioButton"))
82: {
83: if (pi.PropertyType.Name.Equals("System.Int16") || pi.PropertyType.Name.Equals("System.Int32") || pi.PropertyType.Name.Equals("System.Int64"))
84: {
85: if (!pi.PropertyType.IsArray)
86: {
87: (c.GetValue(UI) as RadioButton).Checked = ((int)pi.GetValue(this, null) > 0) ? true : false;
88: }
89: else
90: {
91: object[] arrVal = (object[])pi.GetValue(this, null);
92: (c.GetValue(UI) as RadioButton).Checked = ((int)arrVal[IndexProp] > 0) ? true : false;
93: }
94: }
95: if (pi.PropertyType.Name.Equals("System.Boolean"))
96: {
97: if (!pi.PropertyType.IsArray)
98: {
99: (c.GetValue(UI) as RadioButton).Checked = (bool)pi.GetValue(this, null);
100: }
101: else
102: {
103: object[] arrVal = (object[])pi.GetValue(this, null);
104: (c.GetValue(UI) as RadioButton).Checked = (bool)arrVal[IndexProp];
105: }
106: }
107: if (pi.PropertyType.Name.Equals("System.String"))
108: {
109: if (!pi.PropertyType.IsArray)
110: {
111: (c.GetValue(UI) as RadioButton).Checked = (pi.GetValue(this, null).ToString().ToUpper().Equals("SIM") || pi.GetValue(this, null).ToString().ToUpper().Equals("S"));
112: }
113: else
114: {
115: object[] arrVal = (object[])pi.GetValue(this, null);
116: (c.GetValue(UI) as RadioButton).Checked = ((arrVal[IndexProp].ToString().ToUpper().Equals("SIM") || arrVal[IndexProp].ToString().ToUpper().Equals("S")));
117: }
118: }
119: }
120: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.Label"))
121: {
122: string formatinfo = "";
123: int idx = 0;
124: if (ContainFormatInfo(pi.Name, out idx, out formatinfo))
125: {
126: if (!pi.PropertyType.IsArray)
127: {
128: (c.GetValue(UI) as Label).Text = String.Format(formatinfo, pi.GetValue(this, null));
129: }
130: else
131: {
132: object[] arrVal = (object[])pi.GetValue(this, null);
133: (c.GetValue(UI) as Label).Text = String.Format(formatinfo, arrVal[IndexProp]);
134: }
135: }
136: else
137: {
138: if (!pi.PropertyType.IsArray)
139: {
140: (c.GetValue(UI) as Label).Text = pi.GetValue(this, null).ToString();
141: }
142: else
143: {
144: object[] arrVal = (object[])pi.GetValue(this, null);
145: (c.GetValue(UI) as Label).Text = arrVal[IndexProp].ToString();
146: }
147: }
148: }
149: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.CheckBox"))
150: {
151: if (pi.PropertyType.Name.Equals("System.Int16") || pi.PropertyType.Name.Equals("System.Int32") || pi.PropertyType.Name.Equals("System.Int64"))
152: {
153: if (!pi.PropertyType.IsArray)
154: {
155: (c.GetValue(UI) as CheckBox).Checked = ((int)pi.GetValue(this, null) > 0) ? true : false;
156: }
157: else
158: {
159: object[] arrVal = (object[])pi.GetValue(this, null);
160: (c.GetValue(UI) as CheckBox).Checked = ((int)arrVal[IndexProp] > 0) ? true : false;
161: }
162: }
163: if (pi.PropertyType.Name.Equals("Boolean"))
164: {
165: if (!pi.PropertyType.IsArray)
166: {
167: (c.GetValue(UI) as CheckBox).Checked = (bool)pi.GetValue(this, null);
168: }
169: else
170: {
171: object[] arrVal = (object[])pi.GetValue(this, null);
172: (c.GetValue(UI) as CheckBox).Checked = (bool)arrVal[IndexProp];
173: }
174: }
175: if (pi.PropertyType.Name.Equals("System.String"))
176: {
177: if (!pi.PropertyType.IsArray)
178: {
179: (c.GetValue(UI) as CheckBox).Checked = (pi.GetValue(this, null).ToString().ToUpper().Equals("SIM") || pi.GetValue(this, null).ToString().ToUpper().Equals("S"));
180: }
181: else
182: {
183: object[] arrVal = (object[])pi.GetValue(this, null);
184: (c.GetValue(UI) as CheckBox).Checked = ((arrVal[IndexProp].ToString().ToUpper().Equals("SIM") || arrVal[IndexProp].ToString().ToUpper().Equals("S")));
185: }
186: }
187: }
188: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.CheckBoxList"))
189: {
190: if (!pi.PropertyType.IsArray)
191: {
192: (c.GetValue(UI) as CheckBoxList).SelectedValue = pi.GetValue(this, null).ToString();
193: }
194: else
195: {
196: object[] arrVal = (object[])pi.GetValue(this, null);
197: (c.GetValue(UI) as CheckBoxList).SelectedValue = arrVal[IndexProp].ToString();
198: }
199: }
200: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.RadioButtonList"))
201: {
202: if (!pi.PropertyType.IsArray)
203: {
204: (c.GetValue(UI) as RadioButtonList).SelectedValue = pi.GetValue(this, null).ToString();
205: }
206: else
207: {
208: object[] arrVal = (object[])pi.GetValue(this, null);
209: (c.GetValue(UI) as RadioButtonList).SelectedValue = arrVal[IndexProp].ToString();
210: }
211: }
212: if (c.FieldType.FullName.Equals("System.Web.UI.WebControls.GridView"))
213: {
214: if (!pi.PropertyType.IsArray)
215: {
216: (c.GetValue(UI) as GridView).DataSource = pi.GetValue(this, null);
217: (c.GetValue(UI) as GridView).DataBind();
218: }
219: else
220: {
221: object[] arrVal = (object[])pi.GetValue(this, null);
222: (c.GetValue(UI) as GridView).DataSource = arrVal[IndexProp];
223: (c.GetValue(UI) as GridView).DataBind();
224: }
225: }
226:
227:
228: if (c.FieldType.FullName.Equals("System.Web.UI.HtmlControls.HtmlInputHidden"))
229: {
230: string formatinfo = "";
231: int idx = 0;
232: if (ContainFormatInfo(pi.Name, out idx, out formatinfo))
233: {
234: if (!pi.PropertyType.IsArray)
235: {
236: (c.GetValue(UI) as HtmlInputHidden).Value = String.Format(formatinfo, pi.GetValue(this, null));
237: }
238: else
239: {
240: object[] arrVal = (object[])pi.GetValue(this, null);
241: (c.GetValue(UI) as HtmlInputHidden).Value = String.Format(formatinfo, arrVal[IndexProp]);
242: }
243: }
244: else
245: {
246: if (!pi.PropertyType.IsArray)
247: {
248: (c.GetValue(UI) as HtmlInputHidden).Value = pi.GetValue(this, null).ToString();
249: }
250: else
251: {
252: object[] arrVal = (object[])pi.GetValue(this, null);
253: (c.GetValue(UI) as HtmlInputHidden).Value = arrVal[IndexProp].ToString();
254: }
255: }
256: }
257: }
258: }
259: }