我正在處理的網站將包含一個顯示許多原因代碼的下拉列表。使用數組來填充列表框?
是否可以使用派生自自定義類的數組填充下拉列表?
事情是這樣的:
ASPX頁面
<asp:DropDownList ID="ddlReasonsWhy" runat="server"></asp:DropDownList>
ASPX代碼隱藏
protected void Page_Load(object sender, EventArgs e)
{
//instantiate custom class
Class1 reasonsList = new Class1();
//populate reasons list
this.ddlReasonsWhy.Items.Add(reasonsList.getReasons);
自定義類
public string[] getReasons()
{
string[] Reasons;
Reasons[0] = "test";
Reasons[1] = "test2";
Reasons[2] = "test3";
return Reasons;
}
UPDATE1:
多虧了答案,這個堆link我的最終解決方案類似於艾哈邁德和柔滑的答案。
類
public List<ListItem> getRejectReasons()
{
List<ListItem> reasons = new List<ListItem>();
reasons.Add(new ListItem("Select Value", "0"));
reasons.Add(new ListItem("Red", "1"));
reasons.Add(new ListItem("Green", "2"));
return reasons;
}
代碼隱藏
//instantiate custom class
Class1 reject = new Class1();
this.ddlReasonsWhy.DataSource = reject.getRejectReasons();
this.ddlReasonsWhy.DataBind();
謝謝 - 這非常有幫助 - 請參閱我的update1以獲取正確的類語法。 – 2010-01-07 15:39:29