DropDownList下拉的作法

摘要:DropDownList下拉的作法

詳細做法如下:

.aspx
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="0">請選擇</asp:ListItem>
</asp:DropDownList>
 
.cs 註:DBComm是一個Class,請自行換為您個人的方法去抓資料庫.

protected DataSet CreateSource(string strTableName, string field, string strWhere)
{
	DBComm db = new DBComm(ConfigurationSettings.AppSettings["strConn"]);
	DataSet ds = new DataSet();

	db.TableName = strTableName;
	db.WhereStmt = strWhere;
	ds = db.GetDataSet(field, 0, 0, strTableName);

	db.dbConn.Close();
	db.Dispose();
	db = null;
	if (ds != null)
	{
		if (ds.Tables[0].Rows.Count <= 0)
		{
			return null;
		}
		else
		{
			return ds;
		}
	}
	else
	{
		return null;
	}
}

用法:
 


DropDownList1.DataSource = CreateSource("employee", "name,id", " where valid = 1");
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
DropDownList1.Attributes.Add("onChange", "getList(this.value)");

 

網頁設計 -->找網站銀行

好站推薦:網頁設計 除臭襪 易夏貼 安全帽貼

網頁設計 -->找網站銀行

好站推薦:網頁設計除臭襪