asp.net項(xiàng)目開發(fā)中用到的小技巧
更新時(shí)間:2010年03月10日 23:06:33 作者:
項(xiàng)目中用到的小技巧
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;
}
2 為下拉框綁定枚舉:
復(fù)制代碼 代碼如下:
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;
}
您可能感興趣的文章:
- 創(chuàng)建一個(gè)完整的ASP.NET Web API項(xiàng)目
- asp.net 學(xué)習(xí)之路 項(xiàng)目整體框架簡(jiǎn)單的搭建
- 如何改變asp.net項(xiàng)目名稱
- ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)
- asp.net檢查服務(wù)器上目錄或文件是否存在的方法
- asp.net獲取網(wǎng)站目錄物理路徑示例
- Asp.net獲取當(dāng)前目錄的方法小結(jié)
- asp.net 獲取目錄下的文件數(shù)和文件夾數(shù)
- asp.net Cookie跨域、虛擬目錄等設(shè)置方法
- asp.net編程獲取項(xiàng)目根目錄實(shí)現(xiàn)方法集合
相關(guān)文章
"虛擬路徑"..."映射到另一個(gè)應(yīng)用程序,這是不允許的!
原因: 用戶控件不能跨虛擬目錄調(diào)用。2008-12-12深入淺析ASP在線壓縮access數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了ASP在線壓縮access數(shù)據(jù)庫(kù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09ASP.NET數(shù)據(jù)綁定之DataList控件
這篇文章主要為大家介紹了ASP.NET數(shù)據(jù)綁定中的DataList控件,DataList控件以表的形式呈現(xiàn)數(shù)據(jù),通過該控件,您可以使用不同的布局來顯示數(shù)據(jù)記錄,對(duì)DataList控件感興趣的小伙伴們可以參考一下2016-01-01ASP.NET MVC如何使用Unity實(shí)現(xiàn)Ioc詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET MVC如何使用Unity實(shí)現(xiàn)Ioc的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07GridView自動(dòng)增加序號(hào)(三種實(shí)現(xiàn)方式)
第一種方式,直接在Aspx頁(yè)面GridView模板列中.這種的缺點(diǎn)是到第二頁(yè)分頁(yè)時(shí)又重新開始了,第二種方式分頁(yè)時(shí)進(jìn)行了計(jì)算,這樣會(huì)累計(jì)向下加,點(diǎn)三種放在cs代碼中2013-04-04DAM 簡(jiǎn)單跨數(shù)據(jù)庫(kù)ADO.NET組件
這是一個(gè)可以實(shí)現(xiàn)簡(jiǎn)單跨數(shù)據(jù)庫(kù)基于ADO.NET的組件。您可以在DAL層透過它來訪問數(shù)據(jù)庫(kù)。這是一個(gè)以前寫過一個(gè)小組件的修改版.2011-01-01ASP.NET?Core?MVC控制器請(qǐng)求依賴注入
這篇文章介紹了ASP.NET?Core?MVC控制器請(qǐng)求依賴注入的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04asp.net下生成99個(gè)不同的隨機(jī)數(shù)
asp.net下生成99個(gè)不同的隨機(jī)數(shù)...2007-04-04