我有一個下拉列表我&頁面上想要的列表項是從Web服務器上的本地目錄文件夾...即....asp:DropDownList從本地目錄抓取數據?
T:\窗體 T:\手冊 ŧ :\ SOFTWARE
這是到目前爲止我的代碼...
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo("C:/");
DirectoryInfo[] dirArray = di.GetDirectories();
DropDownList1.DataSource = dirArray;
foreach (DirectoryInfo i in dirArray)
{
DropDownList1.DataTextField = i.FullName;
DropDownList1.DataValueField = i.FullName;
}
}
解決
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo("C:/");
DropDownList1.DataSource = di.GetDirectories();
DropDownList1.DataBind();
foreach (DirectoryInfo i in di.GetDirectories())
{
DropDownList1.DataTextField = i.FullName;
}
}
太感謝你了,我怎麼拿到數值F從一個數組到現在的下拉列表? – bluetickk 2011-06-07 14:30:17
您可以將陣列直接綁定到下拉列表。例如:dropDownList.DataSource = yourFileArray;然後設置DataTextField和DataValueField並調用數據綁定。 – WraithNath 2011-06-07 14:31:39
你還需要幫助嗎? DataTextField和DataValue字段應該是作爲字符串傳遞的屬性的名稱,而不是對象的實際值。例如「FullName」而不是i.FullName – WraithNath 2011-06-07 15:41:30