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 { /// /// 绑定下拉框 /// modify tlq 2012-4-16 /// /// /// 绑定的数据源 /// 显示默认第一项的内容:[----请选择----] /// 绑定的显示名(不写默认DataTable的第二列) /// 绑定的值(不写默认DataTable的第一列) 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 { } } /// /// 绑定下拉框 /// modify tlq 2012-4-16 /// /// /// 绑定的数据源 /// 显示默认第一项的内容:[----请选择----] /// 绑定的显示名(不写默认DataTable的第二列) /// 绑定的值(不写默认DataTable的第一列) 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 { } } } }