Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将结构绑定到DropDownList_C#_Asp.net_Data Binding_Drop Down Menu_Struct - Fatal编程技术网

C# 如何将结构绑定到DropDownList

C# 如何将结构绑定到DropDownList,c#,asp.net,data-binding,drop-down-menu,struct,C#,Asp.net,Data Binding,Drop Down Menu,Struct,我在ASP.NET应用程序中使用C#,有些属性我不想存储在数据库中。我希望为这些属性使用已定义的结构,如下所示: public struct MedicalChartActions { public const int Open = 0; public const int SignOff = 1; public const int Review = 2; } 因此,当我使用MedicalChartActions.Open时,我会得

我在ASP.NET应用程序中使用C#,有些属性我不想存储在数据库中。我希望为这些属性使用已定义的结构,如下所示:

public struct MedicalChartActions
    {
        public const int Open = 0;
        public const int SignOff = 1;
        public const int Review = 2;
    }

因此,当我使用
MedicalChartActions.Open
时,我会得到一个等于“0”的整数值,但是我如何将它绑定到
DropDownList
控件,以便显示变量名?如何通过值获取变量名?例如,如果值等于“0”,如何返回“Open”?

我将使用类似SLaks建议的枚举器,而不是使用结构

public enum MedicalChartActions : int
{ 
    Open = 0,
    SignOff = 1, 
    Review = 2
} 
然后你可以这样做:

var actions = from MedicalChartActions action in Enum.GetValues(typeof(MedicalChartActions))
              select new 
              { 
                  Name = action.ToString(), 
                  Value = (int)action; 
              };

DropDownList1.DataSource = actions.ToList();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
int value = 0;
MedicalChartActions action = (MedicalChartActions)value;

string actionName = action.ToString();    
编辑

将结构更改为枚举后,可以从如下值获取名称:

var actions = from MedicalChartActions action in Enum.GetValues(typeof(MedicalChartActions))
              select new 
              { 
                  Name = action.ToString(), 
                  Value = (int)action; 
              };

DropDownList1.DataSource = actions.ToList();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
int value = 0;
MedicalChartActions action = (MedicalChartActions)value;

string actionName = action.ToString();    

如果是我,您不想点击数据库来加载可能的值,我只需要将这些值硬编码到程序中

首先,以声明方式创建下拉列表,如下所示:

<asp:DropDownList ID="List1" runat="server">
    <asp:ListItem Text="Open" Value="0" />
    <asp:ListItem Text="SignOff" Value="1" />
    <asp:ListItem Text="Review" Value="2" />
</asp:DropDownList>

现在,您可以将这些值引用为MyEnum.Open,而不是0。

Metro?WinForms?WPF?银灯?ASP.Net?MonoTouch?它是一个asp.net web应用程序。我更新了我的帖子。谢谢。你肯定会用反射来做这个。有没有什么原因让你不得不使用结构,而不是将键/值存储在字典或其他东西中?结构部分在这里并不重要——它们是常量这一事实更重要。@MikeChristensen,我想做的是降低数据服务器的成本。不管我是使用struct还是其他什么东西,缺点是你需要在使用enum的地方复制这个DropDownList。是的,我更喜欢你的解决方案。“你把它贴了出来,就像我在贴我的一样。@MikeChristensen,但是我们怎么能在中使用空格呢?”enum@Dragon-你不能。您可能希望为此绑定到字典,或者只是硬编码字符串值。@MikeChristensen如果我在枚举中使用Description属性,比如公共枚举枚举类型{[Description(“新车”)]optionone=1,[Description(“二手车”)]optiontwo=2,[Description],该怎么办(“重型自行车”)]optionTree=3}谢谢,那么当我已经知道“0”时,如何“打开”呢?@james johnson但如果我们在值中有空间呢