实体类赋值给控件,控件赋值给实体类

/// <summary>
/// 绑定实体,把控件装在Panel里
/// </summary>
/// <param name="spanel"></param>
/// <param name="entity"></param>
public void BinToEntity(Panel spanel, object entity)
{
ArrayList al = new ArrayList();
List<PropertyInfo> PI2list = entity.GetType().GetProperties().ToList();
foreach (Control cl in spanel.Controls)
{
if (cl is TextBox)
{
TextBox tb = (TextBox)cl;
string tbName = tb.ID.Replace("txt", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == tbName);
if (tmpPropQuery != null)
{
object dd = tb.Text.Trim();
if (!string.IsNullOrEmpty(tb.Text.Trim()) || tmpPropQuery.PropertyType.Name=="String")
tmpPropQuery.SetValue(entity, Convert.ChangeType(dd, tmpPropQuery.PropertyType), null);
}
}
if (cl is DropDownList)
{
DropDownList dl = (DropDownList)cl;
string dlName = dl.ID.Replace("ddl", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == dlName);
if (tmpPropQuery != null)
{
if (!string.IsNullOrEmpty(dl.SelectedValue))
{
tmpPropQuery.SetValue(entity, Convert.ChangeType(dl.SelectedValue, tmpPropQuery.PropertyType), null);
}
else if (tmpPropQuery.PropertyType.Name == "String")
{
tmpPropQuery.SetValue(entity, "", null);
}
}
}
if (cl is CheckBoxList)
{
CheckBoxList dl = (CheckBoxList)cl;
string dlName = dl.ID.Replace("cbl", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == dlName);
if (tmpPropQuery != null)
{
string dd ="";
for (int i = 0; i < dl.Items.Count; i++)
{
if (dl.Items[i].Selected)
{
dd += dl.Items[i].Value + " ";
}
}
if (tmpPropQuery.PropertyType.Name == "String")
tmpPropQuery.SetValue(entity, Convert.ChangeType(dd, tmpPropQuery.PropertyType), null);
}
}
if (cl is CheckBox)
{
CheckBox cb = (CheckBox)cl;
string Name = cb.ID.Replace("cb", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == Name);
if (tmpPropQuery != null)
{
tmpPropQuery.SetValue(entity, Convert.ChangeType(cb.Checked, tmpPropQuery.PropertyType), null);
}
}
if (cl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)cl;
string Name = rbl.ID.Replace("rbl", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == Name);
if (tmpPropQuery != null)
{
if (!string.IsNullOrEmpty(rbl.SelectedValue))
{
tmpPropQuery.SetValue(entity, Convert.ChangeType(rbl.SelectedValue, tmpPropQuery.PropertyType), null);
}
else if (tmpPropQuery.PropertyType.Name == "String")
{
tmpPropQuery.SetValue(entity, "", null);
}
}
}
}
}

/// <summary>
/// 实体绑定控件把控件装在Panel里
/// </summary>
/// <param name="spanel"></param>
/// <param name="entity"></param>
public void BindControl(Panel spanel, object entity)
{
ArrayList al = new ArrayList();
List<PropertyInfo> PI2list = entity.GetType().GetProperties().ToList();
foreach (Control cl in spanel.Controls)
{
if (cl is TextBox)
{
TextBox tb = (TextBox)cl;
string tbName = tb.ID.Replace("txt", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == tbName);
if (tmpPropQuery != null)
{
tb.Text = tmpPropQuery.GetValue(entity, null).ToString();
}
}
if (cl is DropDownList)
{
DropDownList dl = (DropDownList)cl;
string dlName = dl.ID.Replace("ddl", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == dlName);
if (tmpPropQuery != null)
{
var dd = tmpPropQuery.GetValue(entity, null);
if (dd != null)
{
dl.SelectedIndex = dl.Items.IndexOf(dl.Items.FindByValue(dd.ToString()));
}
}
}
if (cl is CheckBoxList)
{
CheckBoxList dl = (CheckBoxList)cl;
string dlName = dl.ID.Replace("cbl", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == dlName);
if (tmpPropQuery != null)
{
var dd = tmpPropQuery.GetValue(entity, null);
List<string> list = dd.ToString().Split().ToList();
if (list != null && list.Count > 0)
{
for (int i = 0; i < dl.Items.Count; i++)
{
if (list.Contains(dl.Items[i].Value.Trim()))
{
dl.Items[i].Selected = true;
}
}
}
}
}
if (cl is CheckBox)
{
CheckBox cb = (CheckBox)cl;
string Name = cb.ID.Replace("cb", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == Name);
if (tmpPropQuery != null)
{
cb.Checked = bool.Parse(tmpPropQuery.GetValue(entity, null).ToString());
}
}
if (cl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)cl;
string Name = rbl.ID.Replace("rbl", "");
PropertyInfo tmpPropQuery = PI2list.Find(p => p.Name == Name);
if (tmpPropQuery != null)
{
ListItem item=rbl.Items.FindByValue(tmpPropQuery.GetValue(entity, null).ToString());
if(item!=null)
{
rbl.SelectedIndex = rbl.Items.IndexOf(item);
}
}
}
}
}

posted @ 2013-04-22 11:38  文艺流浪汉  阅读(289)  评论(0编辑  收藏  举报