用于下拉选框通用代码

#region 用于下拉选框

        /// <summary>
        /// 通用下拉选框数据绑定
        /// </summary>
        /// <param name="ddl">下拉选框控件</param>
        /// <param name="strList">字符串列表(源)</param>
        /// <param name="TextField">绑定要显示的值</param>
        /// <param name="ValueField">绑定要传递的值</param>
        /// <returns>DropDownList</returns>
        public static DropDownList DropDownListBind(DropDownList ddl, string [] strList, int TextIndex, int ValueIndex)
        {
            DataTable dt = EduCommon.StringListToDataTabel(strList);
            ddl.Items.Clear();
            ddl.Items.Add(new System.Web.UI.WebControls.ListItem("==请选择==", "-99"));
            ddl.AppendDataBoundItems = true;
            ddl.DataSource = dt;
            ddl.DataTextField = dt.Columns[TextIndex].ColumnName;
            ddl.DataValueField = dt.Columns[ValueIndex].ColumnName;
            ddl.DataBind();
            return ddl;
        }

        /// <summary>
        /// 通用下拉选框数据绑定
        /// </summary>
        /// <param name="ddl">下拉选框控件</param>
        /// <param name="dt">数据表(源)</param>
        /// <param name="TextField">绑定要显示的值</param>
        /// <param name="ValueField">绑定要传递的值</param>
        /// <returns>DropDownList</returns>
        public static DropDownList DropDownListBind(DropDownList ddl, DataTable dt, int TextIndex, int ValueIndex)
        {
            ddl.Items.Clear();
            ddl.Items.Add(new System.Web.UI.WebControls.ListItem("==请选择==", "-99"));
            ddl.AppendDataBoundItems = true;
            ddl.DataSource = dt;
            ddl.DataTextField = dt.Columns[TextIndex].ColumnName;
            ddl.DataValueField = dt.Columns[ValueIndex].ColumnName;
            ddl.DataBind();
            return ddl;
        }

        /// <summary>
        /// 通用下拉选框数据绑定
        /// </summary>
        /// <param name="ddl">下拉选框控件</param>
        /// <param name="dt">数据表(源)</param>
        /// <param name="TextField">绑定要显示的值</param>
        /// <param name="ValueField">绑定要传递的值</param>
        /// <returns></returns>
        public static void DropDownListBind(DropDownList ddl, DataTable dt, string TextField, string ValueField)
        {
            ddl.Items.Clear();           
            ddl.Items.Add(new System.Web.UI.WebControls.ListItem("==请选择==", "-99"));
            ddl.AppendDataBoundItems = true;
            ddl.DataSource = dt;
            ddl.DataTextField = dt.Columns[TextField].ColumnName;
            ddl.DataValueField = dt.Columns[ValueField].ColumnName;
            ddl.DataBind();
        }

        /// <summary>
        /// 通用下拉选框数据绑定后值的选定
        /// </summary>
        /// <param name="ddl">下拉选框控件</param>
        /// <param name="selectedValue">要选定的值</param>
        /// <returns></returns>
        public static void DropDownListSelectedValue(DropDownList ddl,string selectedValue)
        {
            for (int i = 0; i < ddl.Items.Count; i++)
            {
                if(ddl.Items[i].Value.Equals(selectedValue.Trim()))
                {
                    ddl.Items[i].Selected=true;
                    break;
                }
            }
        }

        /// <summary>
        /// 通用下拉选框数据绑定后值的选定
        /// </summary>
        /// <param name="ddl">下拉选框控件</param>
        /// <param name="selectedItem">要选定的项</param>
        /// <returns></returns>
        public static void DropDownListSelectedItem(DropDownList ddl, string selectedItem)
        {
            for (int i = 0; i < ddl.Items.Count; i++)
            {
                if (ddl.Items[i].Text.Equals(selectedItem.Trim()))
                {
                    ddl.Items[i].Selected = true;
                }
                else
                {
                    ddl.Items[i].Selected = false;
                }
            }
        }

        #endregion

你可能感兴趣的:(用于下拉选框通用代码)