12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.UI.WebControls;
- using System.Data;
- namespace Bowin.Common
- {
- public static class BindDataTools
- {
- /// <summary>
- /// 绑定下拉框
- /// modify tlq 2012-4-16
- /// </summary>
- /// <param name="ddl"></param>
- /// <param name="T">绑定的数据源</param>
- /// <param name="defaultValue">显示默认第一项的内容:[----请选择----]</param>
- /// <param name="textField">绑定的显示名(不写默认DataTable的第二列)</param>
- /// <param name="valueField">绑定的值(不写默认DataTable的第一列)</param>
- public static void BindDataTable(this DropDownList ddl, DataTable T, string defaultValue = "", string textField = "", string valueField = "", bool IsShowSelected=true)
- {
- try
- {
- if (IsShowSelected)
- {
- if (defaultValue != "")
- {
- ddl.Items.Add(new System.Web.UI.WebControls.ListItem(defaultValue, ""));
- }
- else
- {
- ddl.Items.Add(new System.Web.UI.WebControls.ListItem("===未指定===", ""));
- }
- }
- for (int i = 0; i < T.Rows.Count; i++)
- {
- ddl.Items.Add(new ListItem(textField != "" ? T.Rows[i][textField].ToString() : T.Rows[i][1].ToString(), valueField != "" ? T.Rows[i][valueField].ToString() : T.Rows[i][0].ToString()));
- }
- if (ddl.Items.Count > 0)
- {
- ddl.SelectedIndex = 0;
- }
- }
- catch
- {
- }
- }
-
- /// <summary>
- /// 绑定下拉框
- /// modify tlq 2012-4-16
- /// </summary>
- /// <param name="ddl"></param>
- /// <param name="T">绑定的数据源</param>
- /// <param name="defaultValue">显示默认第一项的内容:[----请选择----]</param>
- /// <param name="textField">绑定的显示名(不写默认DataTable的第二列)</param>
- /// <param name="valueField">绑定的值(不写默认DataTable的第一列)</param>
- public static void BindDataTable(this ListBox ddl, DataTable T, string defaultValue = "", string textField = "", string valueField = "", bool IsShowSelected=true)
- {
- try
- {
- if (IsShowSelected)
- {
- if (defaultValue != "")
- {
- ddl.Items.Add(new System.Web.UI.WebControls.ListItem(defaultValue, ""));
- }
- else
- {
- ddl.Items.Add(new System.Web.UI.WebControls.ListItem("===未指定===", ""));
- }
- }
- for (int i = 0; i < T.Rows.Count; i++)
- {
- ddl.Items.Add(new ListItem(textField != "" ? T.Rows[i][textField].ToString() : T.Rows[i][1].ToString(), valueField != "" ? T.Rows[i][valueField].ToString() : T.Rows[i][0].ToString()));
- }
- if (ddl.Items.Count > 0)
- {
- ddl.SelectedIndex = 0;
- }
- }
- catch
- {
- }
- }
-
- }
- }
|