在dropdownlist中使用enum

平常我们用个dropdownlist,一般都像这样写:
<asp:DropDownList id="xl" runat="server">
          <asp:ListItem Value="0">小学</asp:ListItem>
          <asp:ListItem Value="1">中学</asp:ListItem>
          <asp:ListItem Value="2">大学</asp:ListItem>
</asp:DropDownList>
现在我们可以用枚举来写这样:
先定义一个enum:
public enum 学历
{
    小学  = 0,
  中学 = 1,
  大学  = 2

}

好玩的是.net里还支持中文的变量。
然后根据enum来动态创建dropdownlist:
foreach(学历 _xl in Enum.GetValues(typeof(学历)))
   {
    xl.Items.Add(new ListItem(_xl.ToString(), Convert.ToInt32(_xl).ToString()));
   }
如果我们有多个地方用到同样的dropdownlist时候,那就比较方便了。

 

posted @ 2004-06-15 13:53  Martin XJ  阅读(1319)  评论(3编辑  收藏  举报