以下是两个在ASP.net中从类绑定下拉列表的示例
您的aspx页面
<asp:DropDownList ID="DropDownListJour1" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownListJour2" runat="server">
</asp:DropDownList>
你的aspx.cs页面
protected void Page_Load(object sender, EventArgs e)
{
DropDownListJour1.DataSource = jour.ListSameValueText();
DropDownListJour1.DataBind();
DropDownListJour2.DataSource = jour.ListDifferentValueText();
DropDownListJour2.DataValueField = "Key";
DropDownListJour2.DataTextField = "Value";
DropDownListJour2.DataBind();
}
你的 jour.cs 类(jour.cs)
public class jour
{
public static string[] ListSameValueText()
{
string[] myarray = {"a","b","c","d","e"} ;
return myarray;
}
public static Dictionary<int, string> ListDifferentValueText()
{
var joursem2 = new Dictionary<int, string>();
joursem2.Add(1, "Lundi");
joursem2.Add(2, "Mardi");
joursem2.Add(3, "Mercredi");
joursem2.Add(4, "Jeudi");
joursem2.Add(5, "Vendredi");
return joursem2;
}
}
GetList
返回一个包含5个A的列表,并且这5个A中的每个都包含一个包含5个B的列表,您希望您的DropDownList有5个项目(每个A一个)还是25个项目(每个A中的每个B一个)? - 300 baud