dropdownlist加载方法

1)controlvalue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
/// <summary>
/// Summary description for ControlValue
/// </summary>
    public class ControlValue
    {
        public ControlValue()
        {
        }
        public static void InitDropDownList(System.Web.UI.WebControls.DropDownList ddlList, string controlName,string trantype, bool isShowAll)
        {
            ddlList.Items.Clear();
            eControlValue objValueList = new eControlValue();
            objValueList.ControlName = controlName;
            DataTable dt = objValueList.GeteControlValueS(controlName,trantype);
            ddlList.DataSource = dt;
            ddlList.DataTextField = "code_name";
            ddlList.DataValueField = "code_code";
            ddlList.DataBind();
            if (isShowAll)
            {
                System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("所有", "");
                ddlList.Items.Add(item);
            }
        }
        public static void InitDropDownList(System.Web.UI.WebControls.DropDownList ddlList, string controlName, string sortNo,string trantype, bool isShowAll)
        {
            ddlList.Items.Clear();
            eControlValue objValueList = new eControlValue();
            objValueList.ControlName = controlName;
            DataTable dt = objValueList.GeteControlValueS(controlName,sortNo,trantype);
            ddlList.DataSource = dt;
            ddlList.DataTextField = "code_name";
            ddlList.DataValueField = "code_code";
            ddlList.DataBind();
            if (isShowAll)
            {
                System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("所有", "");
                ddlList.Items.Add(item);
            }
        }
    }
2)econtrolvalue

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Data;
/// <summary>
/// Summary description for eControlValue

    public class eControlValue
    {
        protected string _controlName = String.Empty;
        protected string _controlValue = String.Empty;
        public eControlValue()
        {

        }
        public eControlValue(string ControlName, string ControlValue)
        {

        }

        protected void LoadFromReader(SqlDataReader reader)
        {

        }
        public DataTable GeteControlValueS(string code_filed,string trantype)
        {
            string conString = ConfigurationManager.ConnectionStrings["eHRCONNECTIONSTRING"].ConnectionString;
            SqlDataReader dr = null;
            SqlConnection myCon = new SqlConnection(conString);
            SqlCommand myCmd = new SqlCommand("GetCodeDesc_SP", myCon);
            myCmd.CommandType = System.Data.CommandType.StoredProcedure;
            myCmd.Parameters.AddWithValue("@code_field", code_filed);
            myCmd.Parameters.AddWithValue("@TranType", trantype);
            myCon.Open();
            dr = myCmd.ExecuteReader();
            return CreateDataTableData(dr);
        }
        public DataTable GeteControlValueS(string code_filed,string code_sortno,string trantype)
        {
            string conString = ConfigurationManager.ConnectionStrings["eHRCONNECTIONSTRING"].ConnectionString;
            SqlDataReader dr = null;
            SqlConnection myCon = new SqlConnection(conString);
            SqlCommand myCmd = new SqlCommand("GetCodeDescBySortNo_SP", myCon);
            myCmd.CommandType = System.Data.CommandType.StoredProcedure;
            myCmd.Parameters.AddWithValue("@code_field", code_filed);
            myCmd.Parameters.AddWithValue("@code_sortno", code_sortno);
            myCmd.Parameters.AddWithValue("@trantype", trantype);
            myCon.Open();
            dr = myCmd.ExecuteReader();
            return CreateDataTableData(dr);
        }
        private DataTable CreateDataTableData(SqlDataReader dr)
        {
            DataTable dt = new DataTable();
            ///创dt的列
            foreach (DataRow row in dr.GetSchemaTable().Rows)
            {   ///需要列的名称和数据类型
                dt.Columns.Add(new DataColumn(row["ColumnName"].ToString(), (Type)row["DataType"]));
            }
            ///读取数据
            while (dr.Read())
            {   ///创建一个新行
                DataRow newrow = dt.NewRow();
                foreach (DataRow row in dr.GetSchemaTable().Rows)
                {   ///读取数据
                    newrow[row["ColumnName"].ToString()] = dr[row["ColumnName"].ToString()];
                }
                ///添加新行
                dt.Rows.Add(newrow);
            }
            ///关闭读取器
            dr.Close();
            return (dt);
        }
        public string ControlName
        {
            get { return _controlName; }
            set { _controlName = value; }
        }
        public string ControlValue
        {
            get { return _controlValue; }
            set { _controlValue = value; }
        }

    }

你可能感兴趣的:(list)