绑定表单域值实用技巧备忘

 public void ControlsSetValue(string ControlsId, object Value)
    {
        var Controls = Page.FindControl(ControlsId);
        if (Controls != null)
        {
            if (typeof(CheckBox) == Controls.GetType())
            {
                ((CheckBox)Controls).Checked = Value.ToString() == "1" ? true : false;
            }
            else if (typeof(TextBox) == Controls.GetType())
            {
                ((TextBox)Controls).Text = Value.ToString();
            }
        }

    }
    public string ControlsGetValue(string ControlsId)
    {
        var Controls = Page.FindControl(ControlsId);
        string Value = "";
        if (Controls != null)
        {
            if (typeof(CheckBox) == Controls.GetType())
            {
                Value = ((CheckBox)Controls).Checked == true ? "1" : "0";
            }
            else if (typeof(TextBox) == Controls.GetType())
            {
                Value = ((TextBox)Controls).Text;
            }
        }
        return Value;
    }
     foreach (DataRow dr in dt.Rows) { 
            foreach (DataColumn dc in dt.Columns) {
                ControlsSetValue("old_"+dc.ColumnName, dr[dc.ColumnName]); 
            }   
    public string GetValue<T>(T t)
    {
        string tStr = string.Empty;
        if (t == null)
        {
            return tStr;
        }
        System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
        if (properties.Length <= 0)
        {
            return tStr;
        }
        foreach (System.Reflection.PropertyInfo item in properties)
        {
            string name = item.Name; //名称
            object value = item.GetValue(t, null);  //值
            ControlsSetValue(name, value.ToString());
        }
        return tStr;
    } 
    public string SetValue<T>(T objSrc)
    {
        string valueTo = "";
        string tStr = string.Empty;
        if (objSrc == null)
        {
            return tStr;
        }
        System.Reflection.PropertyInfo[] properties = objSrc.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
        if (properties.Length <= 0)
        {
            return tStr;
        }
        foreach (System.Reflection.PropertyInfo info in properties)
        {
            string name = info.Name; //名称  
            valueTo = ControlsGetValue(name);
            if (info != null)//存在而且值不相等,就修改
            {
                object objValue = info.GetValue(objSrc, null);
                string valueSrc = objValue == null ? "" : objValue.ToString();

                if (info.PropertyType == typeof(System.Decimal))
                {
                    info.SetValue(objSrc, Convert.ToDecimal(valueTo), null);
                }
                else if (info.PropertyType == typeof(System.Int32))
                {
                    info.SetValue(objSrc, Convert.ToInt32(valueTo), null);
                }
                else if (info.PropertyType == typeof(System.String))
                {
                    info.SetValue(objSrc, valueTo, null);
                }
                else if (info.PropertyType == typeof(System.DateTime))
                {
                    if (valueTo == "")
                    {
                        info.SetValue(objSrc, null, null);
                    }
                    else
                    {
                        info.SetValue(objSrc, Convert.ToDateTime(valueTo), null);
                    }
                }

            }
        }

        return tStr; 
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值