欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net项目开发中用到的小技巧

程序员文章站 2024-03-07 23:26:33
1 显示枚举的值:<%# (cn80s.ddpm.model.enum.enumbidcardstatus)(int)eval("perpaidcard_status...
1 显示枚举的值:<%# (cn80s.ddpm.model.enum.enumbidcardstatus)(int)eval("perpaidcard_status")%>
2 为下拉框绑定枚举:
复制代码 代码如下:

getenumlist(ddlbids);
void getenumlist(dropdownlist ddl)
{
foreach (enumbidcardtype s in system.enum.getvalues(typeof(enumbidcardtype)))
{
ddl.items.add(new listitem(s.tostring(), ((int)s).tostring()));
}
}
this.ddlbids.datasource = getenumlist(typeof(enumbidcardtype), true);
this.ddlbids.datatextfield = "text";
this.ddlbids.datavaluefield = "value";
this.ddlbids.databind();
public static list<listitem> getenumlist(type enumtype, bool allalloption)
{
if (enumtype.isenum == false)
{
return null;
}
list<listitem> list = new list<listitem>();
if (allalloption == true)
{
list.add(new listitem("--全部--", ""));
}
type typedescription = typeof(descriptionattribute);
system.reflection.fieldinfo[] fields = enumtype.getfields();
string strtext = string.empty;
string strvalue = string.empty;
foreach (fieldinfo field in fields)
{
if (field.isspecialname) continue;
strvalue = field.getrawconstantvalue().tostring();
object[] arr = field.getcustomattributes(typedescription, true);
if (arr.length > 0)
{
strtext = (arr[0] as descriptionattribute).description;
}
else
{
strtext = field.name;
}
list.add(new listitem(strtext, strvalue));
}
return list;
}