zoukankan      html  css  js  c++  java
  • 实体类赋值给控件,控件赋值给实体类

    /// <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);
    }
    }
    }
    }
    }

  • 相关阅读:
    [BZOJ1193][HNOI2006]马步距离 大范围贪心小范围爆搜
    [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树
    [BZOJ1069][SCOI2007]最大土地面积 凸包+旋转卡壳
    旋转卡壳 求凸多边形中面积最大的四边形
    [BZOJ2815][ZJOI2012]灾难 灭绝树+拓扑排序+lca
    [BZOJ2599][IOI2011]Race 点分治
    [BZOJ1455]罗马游戏 左偏树+并查集
    [BZOJ1295][SCOI2009]最长距离 最短路+枚举
    [LintCode] Climbing Stairs
    [Codeforces] MultiSet
  • 原文地址:https://www.cnblogs.com/lucoo/p/3035196.html
Copyright © 2011-2022 走看看