zoukankan      html  css  js  c++  java
  • 把数据显示到页面和从页面取数据的方法

        //数据显示到页面
        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;

        }
  • 相关阅读:
    ASP.NET -- repeater控件的使用
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40
    错误提示:在此上下文中不允许使用名称 "***"。有效表达式包括常量、 常量表达式和变量(在某些上下文中),不允许使用列名。
    mongodb 性能提高之利用索引, 待续
    工程化 经历的 4 个阶段
    把连续的字符 变成 一个
    sort 排序详解
    理解正则 的 ?! ?:
    [ 订单查询 ] 性能 高并发 : 分表 与 用户id%1024 存放表
    搭建LNMP基础框架
  • 原文地址:https://www.cnblogs.com/zhukezhuke/p/1534711.html
Copyright © 2011-2022 走看看