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

  • 相关阅读:
    【leetcode】9 longest common prefix
    4月份需要整理的问题总结
    JavaScript DOM 编程艺术(第2版)---P1~P9
    JavaScript DOM 编程艺术(第2版)---序中故
    18年3月周末问题汇总
    三目运算+传参+封装的运用实例
    git学习参考博客
    jq实现类名(class)的增删改查
    ztree树集使用(2)
    word2010在正常关闭的情况下,点了“不保存”,如何恢复
  • 原文地址:https://www.cnblogs.com/lucoo/p/3035196.html
Copyright © 2011-2022 走看看