asp.net web控件数据绑定

 public class FillControl
    {


        /// <summary>
        /// 遍历页面,给TextBox控件绑定数据
        ///  使用方式:BindDataToTextBox(this.Page, l[0]);
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="entity">实体对象</param>
        public void BindDataToTextBox(System.Web.UI.Control control, object entity)
        {
            bool isProperty = false;  //当前控件是否对应于当前实体属性
            object temp = null;
            foreach (System.Web.UI.Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    //temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity);
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    if (temp.GetType() == typeof(DateTime))
                    {
                        if (Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd").Equals("1755-01-01"))
                        {
                            ((TextBox)c).Text = "";
                        }
                        else
                        {
                            //((TextBox)c).Text = Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd");
                            if (temp.ToString().Length > 10 && Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd HH:mm:ss").IndexOf("00:00:00") > 0)
                            {
                                ((TextBox)c).Text = Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd");
                            }
                            else
                            {
                                ((TextBox)c).Text = Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                            }
                        }
                    }
                    else
                    {
                        ((TextBox)c).Text = temp.ToString();
                    }
                }
                if (c.HasControls())
                {
                    BindDataToTextBox(c, entity);
                }
            }
        }


        /// <summary>
        /// 根据实体属性的值给控件名/ID赋值
        /// </summary>
        /// <param name="controlID">控件id</param>
        /// <param name="entity">实体</param>
        /// <param name="isProperty">是否属于实体属性</param>
        /// <returns></returns>
        private object GetValueFromEntity(string controlID, object entity, out bool isProperty)
        {
            isProperty = false;
            object fieldValue = DBNull.Value;
            Type t = entity.GetType();
            FieldInfo[] fld = t.GetFields();
            PropertyInfo[] prp = t.GetProperties();

            if (fld == null && prp == null)
            {
                throw new Exception("根据实体属性的值给控件名/ID赋值异常");
            }
            if (fld != null)
            {
                foreach (FieldInfo f in fld)
                {

                    if (f.Name.Equals(controlID, StringComparison.CurrentCultureIgnoreCase))  //区分大小写
                    {
                        fieldValue = f.GetValue(entity);
                        isProperty = true;

                    }
                }
            }
            if (prp != null)
            {
                foreach (PropertyInfo f in prp)
                {

                    if (f.Name.Equals(controlID, StringComparison.CurrentCultureIgnoreCase))  //区分大小写
                    {
                        fieldValue = f.GetValue(entity, null);
                        isProperty = true;

                    }
                }
            }
            if (fieldValue != null)
                return fieldValue;
            else
                return "";
        }



        /// <summary>
        /// 给实体属性赋值:由TextBox、DropDownLis控件以及自定义时间控件InputCalendar、可编辑下拉框EdiDDlist值
        /// 使用方式:SetEntityForTextBoxAndDropDownList(this.Page, l[0]);
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="entity">实体对象</param>
        public void SetEntityForTextBoxAndDropDownList(System.Web.UI.Control control, object entity)
        {
            foreach (System.Web.UI.Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    SetValueFromEntity(c.ClientID.ToString().Substring(3), ((TextBox)c).Text.Trim(), entity);
                }
                if (c is DropDownList)
                {
                    if (((DropDownList)c).SelectedItem != null)
                    {
                        SetValueFromEntity(c.ClientID.ToString().Substring(3), ((DropDownList)c).SelectedItem.Value.ToString(), entity);
                    }
                }
                if (c.HasControls())
                {
                    SetEntityForTextBoxAndDropDownList(c, entity);
                }
            }
        }


        /// <summary>
        /// 根据控件名/ID给实体属性赋值
        /// </summary>
        /// <param name="controlID"></param>
        /// <param name="val"></param>
        /// <param name="entity"></param>
        private void SetValueFromEntity(string controlID, object val, object entity)
        {
            if (val != null && val.ToString().Trim() != "")
            {
                Type t = entity.GetType();
                FieldInfo[] fld = t.GetFields();
                PropertyInfo[] prp = t.GetProperties();
                if (fld == null && prp == null)
                {
                    throw new Exception("根据实体属性的值给控件名/ID赋值异常");
                }
                if (fld != null)
                {
                    foreach (FieldInfo f in fld)
                    {

                        if (f.Name.Equals(controlID, StringComparison.CurrentCultureIgnoreCase))  //区分大小写
                        {

                            if (f.FieldType == typeof(int))
                            { val = Convert.ToInt32(val); }
                            else if (f.FieldType == typeof(decimal))
                            { val = Convert.ToDecimal(val); }
                            else if (f.FieldType == typeof(bool))
                            { val = Convert.ToBoolean(val); }
                            else if (f.FieldType == typeof(double))
                            { val = Convert.ToDouble(val); }
                            else if (f.FieldType == typeof(Single))
                            { val = Convert.ToSingle(val); }
                            else if (f.FieldType == typeof(float))
                            { val = Convert.ToSingle(val); }
                            else if (f.FieldType == typeof(DateTime))
                            { val = Convert.ToDateTime(val); }
                            else if (f.FieldType == typeof(string))
                            { val = val.ToString(); }
                            f.SetValue(entity, val);
                        }
                    }
                }
                if (prp != null)
                {
                    foreach (PropertyInfo f in prp)
                    {

                        if (f.Name.Equals(controlID, StringComparison.CurrentCultureIgnoreCase))  //区分大小写
                        {

                            if (f.PropertyType == typeof(int))
                            { val = Convert.ToInt32(val); }
                            else if (f.PropertyType == typeof(decimal))
                            { val = Convert.ToDecimal(val); }
                            else if (f.PropertyType == typeof(bool))
                            { val = Convert.ToBoolean(val); }
                            else if (f.PropertyType == typeof(double))
                            { val = Convert.ToDouble(val); }
                            else if (f.PropertyType == typeof(Single))
                            { val = Convert.ToSingle(val); }
                            else if (f.PropertyType == typeof(float))
                            { val = Convert.ToSingle(val); }
                            else if (f.PropertyType == typeof(DateTime))
                            { val = Convert.ToDateTime(val); }
                            else if (f.PropertyType == typeof(string))
                            { val = val.ToString(); }
                            f.SetValue(entity, val, null);
                        }

                    }
                }
            }
        }


        /// <summary>
        /// 遍历页面,给TextBox、DropDownList、CheckBox、RadioButton、CheckBoxList、RadioButtonList控件绑定数据
        /// 同时包括自定义时间控件InputCalendar、可编辑下拉框EdiDDlist
        /// 采用Value验证方式。对于赋值给CheckBoxList类型控件,entity对应的属性如果有多选项,则以“;”分开
        /// 使用方式:BindTextBoxAndDropDownList(this.Page, l[0]);
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="entity">实体对象</param>
        public void BindDataToMixControls2(System.Web.UI.Control control, object entity)
        {
            bool isProperty = false;  //当前控件是否对应于当前实体属性
            object temp = null;
            foreach (System.Web.UI.Control c in control.Controls)
            {
                if (c is DropDownList)
                {
                    DropDownList drp = (DropDownList)c;
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    foreach (ListItem l in drp.Items)
                    {
                        l.Selected = false;
                        if (l.Value == temp.ToString())
                        {
                            l.Selected = true;
                        }
                    }
                }
                if (c is TextBox)
                {
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    if (temp.GetType() == typeof(DateTime))
                    {
                        if (Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd").Equals("1755-01-01"))
                        {
                            ((TextBox)c).Text = "";
                        }
                        else
                        {
                            if (temp.ToString().Length > 10 && Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd HH:mm:ss").IndexOf("00:00:00") > 0)
                            {
                                ((TextBox)c).Text = Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd");
                            }
                            else
                            {
                                ((TextBox)c).Text = Convert.ToDateTime(temp.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                            }
                        }
                    }
                    else
                    {
                        ((TextBox)c).Text = temp.ToString();
                    }
                }
                if (c is CheckBox)
                {
                    bool ischeck = false;
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    if (temp != null && temp.ToString().Trim() != "")
                    {
                        switch (temp.ToString().Trim().ToUpper())
                        {
                            case "1":
                                ischeck = true;
                                break;
                            case "0":
                                ischeck = false;
                                break;
                            case "T":
                                ischeck = true;
                                break;
                            case "Y":
                                ischeck = false;
                                break;
                            case "TRUE":
                                ischeck = true;
                                break;
                            case "FALSE":
                                ischeck = false;
                                break;
                        }
                    }
                    if (ischeck == true)
                    {
                        ((CheckBox)c).Checked = true;
                    }
                }
                if (c is RadioButton)
                {
                    bool ischeck = false;
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    if (temp != null && temp.ToString().Trim() != "")
                    {
                        switch (temp.ToString().Trim().ToUpper())
                        {
                            case "1":
                                ischeck = true;
                                break;
                            case "0":
                                ischeck = false;
                                break;
                            case "T":
                                ischeck = true;
                                break;
                            case "Y":
                                ischeck = false;
                                break;
                            case "TRUE":
                                ischeck = true;
                                break;
                            case "FALSE":
                                ischeck = false;
                                break;
                        }
                    }
                    if (ischeck == true)
                    {
                        ((RadioButton)c).Checked = true;
                    }
                }
                if (c is CheckBoxList)
                {
                    CheckBoxList cbl = (CheckBoxList)c;
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    string[] strval = temp.ToString().Split(new char[] { ';' });
                    foreach (ListItem l in cbl.Items)
                    {
                        l.Selected = false;
                        foreach (string s in strval)
                        {
                            if (l.Value == s)  //temp.ToString()
                            {
                                l.Selected = true;
                            }
                        }
                    }
                }
                if (c is RadioButtonList)
                {
                    RadioButtonList cbl = (RadioButtonList)c;
                    isProperty = false;
                    temp = GetValueFromEntity(c.ClientID.ToString().Substring(3), entity, out isProperty);
                    if (isProperty == false)  //与本实体不相应的控件不做处理
                        continue;
                    foreach (ListItem l in cbl.Items)
                    {
                        l.Selected = false;
                        if (l.Value == temp.ToString())
                        {
                            l.Selected = true;
                        }
                    }
                }
                if (c.HasControls())
                {
                    BindDataToMixControls2(c, entity);
                }
            }
        }

        /// <summary>
        /// 给实体属性赋值:由TextBox、DropDownList、CheckBox、RadioButton、CheckBoxList、RadioButtonList以及Lable控件值
        /// 同时包括自定义时间控件InputCalendar、可编辑下拉框EdiDDlist
        /// 对于CheckBoxList类型控件,entity对应的属性如果有多选项,则以“;”分开
        /// 使用方式:SetEntityForTextBoxAndDropDownList(this.Page, l[0]);
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="entity">实体对象</param>
        public void SetEntityForMixControls2(System.Web.UI.Control control, object entity)
        {
            foreach (System.Web.UI.Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    SetValueFromEntity(c.ClientID.ToString().Substring(3), ((TextBox)c).Text.Trim(), entity);
                }
                if (c is DropDownList)
                {
                    if (((DropDownList)c).SelectedItem != null)
                    {
                        SetValueFromEntity(c.ClientID.ToString().Substring(3), ((DropDownList)c).SelectedItem.Value.ToString(), entity);
                    }
                }
                if (c is CheckBox)
                {
                    SetValueFromEntity(c.ClientID.ToString().Substring(3), ((CheckBox)c).Checked, entity);
                }
                if (c is RadioButton)
                {
                    SetValueFromEntity(c.ClientID.ToString().Substring(3), ((RadioButton)c).Checked, entity);
                }
                if (c is CheckBoxList)
                {
                    StringBuilder val = new StringBuilder();
                    foreach (ListItem item in ((CheckBoxList)c).Items)
                    {
                        if (item.Selected == true)
                        {
                            val.Append(";");
                            val.Append(item.Value.Trim());
                        }
                    }
                    if (val.ToString() != "")
                    {
                        SetValueFromEntity(c.ClientID.ToString().Substring(3), val.ToString().Substring(1), entity);
                    }
                }
                if (c is RadioButtonList)
                {
                    if (((RadioButtonList)c).SelectedItem != null)
                    {
                        SetValueFromEntity(c.ClientID.ToString().Substring(3), ((RadioButtonList)c).SelectedItem.Value.ToString(), entity);
                    }
                }
                if (c.HasControls())
                {
                    SetEntityForMixControls2(c, entity);
                }
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ligengdipan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值