C#代码总结03---通过获取类型,分类对前台页面的控件进行赋值操作

该方法: 一般用于将数据库中的基本信息字段显示到前台页面对应的字段控件中

private void InitViewZc(XxEntity model)
    {
        foreach (var info in model.GetType().GetProperties())
        {
            if (info.GetValue(model, null) != null && "EntityState" != info.Name && "EntityKey" != info.Name)
            {
                TextBox cTb = (TextBox)FindControl(info.Name + "Tb");
                TextBox Ipt = (TextBox)FindControl(info.Name + "Ipt");
                TextBox Dec = (TextBox)FindControl(info.Name + "Dec");
                DropDownList cDdl = (DropDownList)FindControl(info.Name + "Ddl");
                RadioButtonList cRbl = (RadioButtonList)FindControl(info.Name + "Rbl");
                if (cTb != null)
                {
                    cTb.Text = info.GetValue(model, null).ToString();
                }
                if (Ipt != null)
                {
                    Ipt.Text = Convert.ToDateTime(info.GetValue(model, null)).ToString("yyyy-MM-dd");
                }
                if (Dec != null)
                {
                    Dec.Text = info.GetValue(model, null).ToString();
                }
                if (cDdl != null)
                {
                    cDdl.Text = info.GetValue(model, null).ToString();
                }
                if (cRbl != null)
                {
                    cRbl.Text = info.GetValue(model, null).ToString();
                }
            }
        }
    }

三个 TextBox控件适用于区分 String,Datetime,Decimal 三种类型格式。

原文地址:https://www.cnblogs.com/JesseP/p/10681236.html