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

  • 相关阅读:
    20172304 2017-2018-2 《程序设计与数据结构》第五周学习总结
    20172304 2017-2018-2 《程序设计与数据结构》第四周学习总结
    20172304 2017-2018《程序设计与数据结构》实验1报告
    20172304 2017-2018-2《程序设计与数据结构》 第3周学习总结
    20172304 2017-2018-2 《程序设计与数据结构》第二周学习总结
    20172304 2017-2018-2《程序结构与程序设计》第一周学习内容总结
    Ubuntu18.04安装教程(一)
    云计算与信息安全第十堂课
    操作系统第十堂课20210510
    云计算与信息安全第八堂课20210427
  • 原文地址:https://www.cnblogs.com/lucoo/p/3035196.html
Copyright © 2011-2022 走看看