您可以在後面的代碼中動態生成項目並將其添加到列表中。爲此,您需要將runat="server"
屬性添加到您的select
,或使用asp:DropDownList
服務器控件。
它看起來像你逐字地插入到列表中的內容。您可能需要將其更改爲更加結構化的數據,或者至少將其作爲結構化數據讀取。
例子:
<%-- in markup of control/page --%>
<asp:DropDownList runat="server" id="ddlCatalogs" />
// in codebehind
// to make this work, you'll need to read the file one line/item as a time
foreach(var item in listOfItemsInFile){
var li = new ListItem();
// TODO: populate "li" from the item's data
// add it to the server control's collection...now it's a part of the ASP.Net
// page lifecycle
this.ddlCatalogs.Controls.Add(option);
}
一旦你治療的下拉作爲服務器控件,您可以通過多種方式(默認的ViewState會照顧它保持狀態,但在列表中的許多項目ViewState將變得相當大)。
或者,您可以採用不同的方向(不涉及服務器控件),只需查看原始Request.Form
集合以查看它是否包含選擇列表的數據,然後將其寫回到頁面中。
即使在這種情況下,可能需要以更結構化的方式枚舉輸入文件,以便您可以選擇正確的選項。
謝謝!我現在使用並在後面的代碼中生成文本和值。只是不知道如何從這裏開始.. –
samoncode
2013-02-13 11:21:27