• 把数据显示到页面和从页面取数据的方法


        //数据显示到页面
        protected void BindDataToPage(Asset act)
        {

            var panelContent = Page.FindControl("PanelAsset");
            Control ctrlFounded;
            TextBox txtField;
            DropDownList ddlField;
            CheckBox chkField;
            string strTemp;

            // 设置控件的值
            Type actType = act.GetType();
            PropertyInfo[] properties = actType.GetProperties();
            foreach (PropertyInfo p in properties)
            {
                //找下拉框
                ctrlFounded = panelContent.FindControl("DDL" + p.Name);
                if (ctrlFounded != null)
                {
                    if (ctrlFounded.GetType() == typeof(DropDownList))
                    {
                        ddlField = (DropDownList)ctrlFounded;
                        strTemp = p.GetValue(act, null).ToString();
                        ddlField.SelectedValue = strTemp;
                    }
                    continue;
                }
                //再找文本框
                ctrlFounded = panelContent.FindControl("Txt" + p.Name);
                if (ctrlFounded != null)
                {
                    if (ctrlFounded.GetType() == typeof(TextBox))
                    {
                        txtField = (TextBox)ctrlFounded;
                        txtField.Text = p.GetValue(act, null).ToString();
                    }
                    continue;
                }
                //再找单选框
                ctrlFounded = panelContent.FindControl("Chk" + p.Name);
                if (ctrlFounded != null)
                {
                    if (ctrlFounded.GetType() == typeof(CheckBox))
                    {
                        chkField = (CheckBox)ctrlFounded;
                        chkField.Checked = (bool)p.GetValue(act, null);
                    }
                    continue;
                }
                //找不到,返回
            }
        }
        //从页面取数据
        protected Asset GetDataFormPage(Asset act)
        {
            var panelContent = Page.FindControl("PanelAsset");
            Control ctrlFounded;
            TextBox txtField;
            DropDownList ddlField;
            CheckBox chkField;
            string strTemp;

            // 从控件的返回值
            Type actType = act.GetType();
            PropertyInfo[] properties = actType.GetProperties();
            foreach (PropertyInfo p in properties)
            {
                //找下拉框
                ctrlFounded = panelContent.FindControl("DDL" + p.Name);
                if (ctrlFounded != null)
                {
                    if (ctrlFounded.GetType() == typeof(DropDownList))
                    {
                        ddlField = (DropDownList)ctrlFounded;

                        if (p.PropertyType.Name == "Boolean")
                        {
                            p.SetValue(act, ddlField.SelectedValue == "0" ? false : true, null);
                        }
                        else
                        {
                            p.SetValue(act, Convert.ChangeType(ddlField.SelectedValue, p.PropertyType), null);
                        }

                    }
                    continue;
                }
                //再找文本框
                ctrlFounded = panelContent.FindControl("Txt" + p.Name);
                if (ctrlFounded != null)
                {
                    if (ctrlFounded.GetType() == typeof(TextBox))
                    {
                        txtField = (TextBox)ctrlFounded;
                        p.SetValue(act, Convert.ChangeType(txtField.Text, p.PropertyType), null);

                    }
                    continue;
                }
                //再找单选框
                ctrlFounded = panelContent.FindControl("Chk" + p.Name);
                if (ctrlFounded != null)
                {
                    if (ctrlFounded.GetType() == typeof(CheckBox))
                    {
                        chkField = (CheckBox)ctrlFounded;
                        p.SetValue(act, Convert.ChangeType(chkField.Checked, p.PropertyType), null);

                    }
                    continue;
                }
                //找不到,返回
            }


            return act;

        }
  • 相关阅读:
    单例模式
    dubbo环境搭建
    树莓派Zero W无键盘无屏幕初始化
    Arduino连接MPU6050陀螺仪
    Raspberry Pi3驱动Oled ssh1106屏
    SonarQube 7.7默认数据库连接方法
    解决SonarQube启动时直接挂掉问题
    解决sonar的ES无法启动问题
    Jenkins自动打包并部署(以java -jar形势运行)
    python3中OpenCV imwrite保存中文路径文件
  • 原文地址:https://www.cnblogs.com/zhukezhuke/p/1534711.html
Copyright © 2020-2023  润新知