将枚举绑定到 AspxListBox
Bind enum to AspxListBox
我有以下 enum
叫 BillTypes
:
public enum BillTypes
{
[EnumProperties("Natural Gas")]
NaturalGas= 1,
[EnumProperties("Electric")]
Electric = 2,
[EnumProperties("Water")]
Water = 3
}
如何将此 enum
绑定到 AspxListBox
?
我认为你应该能够运行以下内容:(如果代码不起作用,请耐心等待,因为我不熟悉ASP
)
BillTypes b = BillTypes.Electric;
AspxListBox alb = new AspxListBox();
alb.Items.Add(BillTypes.Natural_Gas.ToString().Replace("_", " "));
alb.Items.Add(BillTypes.Electric.ToString().Replace("_", " "));
alb.Items.Add(BillTypes.Water.ToString().Replace("_", " "));
alb.SelectedIndexChanged += (ob, ex) => (IndexChanged());
和方法IndexChanged
:
public void IndexChanged()
{
b = (BillTypes)(alb.SelectedIndex + 1);
// here you can do whatever you want...
}
注意稍作编辑 enum
class:
public enum BillTypes
{
[EnumProperties("Natural Gas")]
Natural_Gas= 1,
[EnumProperties("Electric")]
Electric = 2,
[EnumProperties("Water")]
Water = 3
}
在此之后,您可以根据变量 BillTypes
b
.
处理您使用的每个代码
我有以下 enum
叫 BillTypes
:
public enum BillTypes
{
[EnumProperties("Natural Gas")]
NaturalGas= 1,
[EnumProperties("Electric")]
Electric = 2,
[EnumProperties("Water")]
Water = 3
}
如何将此 enum
绑定到 AspxListBox
?
我认为你应该能够运行以下内容:(如果代码不起作用,请耐心等待,因为我不熟悉ASP
)
BillTypes b = BillTypes.Electric;
AspxListBox alb = new AspxListBox();
alb.Items.Add(BillTypes.Natural_Gas.ToString().Replace("_", " "));
alb.Items.Add(BillTypes.Electric.ToString().Replace("_", " "));
alb.Items.Add(BillTypes.Water.ToString().Replace("_", " "));
alb.SelectedIndexChanged += (ob, ex) => (IndexChanged());
和方法IndexChanged
:
public void IndexChanged()
{
b = (BillTypes)(alb.SelectedIndex + 1);
// here you can do whatever you want...
}
注意稍作编辑 enum
class:
public enum BillTypes
{
[EnumProperties("Natural Gas")]
Natural_Gas= 1,
[EnumProperties("Electric")]
Electric = 2,
[EnumProperties("Water")]
Water = 3
}
在此之后,您可以根据变量 BillTypes
b
.