c# – 如何将结构绑定到DropDownList
发布时间:2020-12-15 17:20:49 所属栏目:百科 来源:网络整理
导读:我在我的ASP.NET应用程序中使用C#,并且有些属性我不想存储在数据库中.我想为这些属性使用定义的结构,如下所示: public struct MedicalChartActions { public const int Open = 0; public const int SignOff = 1; public const int Review = 2; } 所以当我使
我在我的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”,如何返回“打开”? 解决方法
我会使用像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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |