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)文章
ASP.NET一次性對(duì)GridView批量更新多行數(shù)據(jù)
這篇文章介紹了ASP.NET一次性對(duì)GridView批量更新多行數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
asp.net實(shí)現(xiàn)圖片以二進(jìn)制流輸出的兩種方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)圖片以二進(jìn)制流輸出的兩種方法,以簡(jiǎn)單實(shí)例形式分析了asp.net實(shí)現(xiàn)以二進(jìn)制流形式讀寫圖片文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
ASP.Net 圖片存入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
在很多時(shí)候,我們有這樣的需求:把圖片存入到數(shù)據(jù)庫(kù)當(dāng)中。在一些應(yīng)用程序中,我們可能有一些敏感的資料,由于存儲(chǔ)在文件系統(tǒng)(file system)中的東西,將很容易被某些用戶盜取,所以這些數(shù)據(jù)不能存放在文件系統(tǒng)中。2008-11-11
asp.net(C#) 動(dòng)態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)
在開發(fā)程序時(shí),有時(shí)需要?jiǎng)討B(tài)添加標(biāo)簽,而有部分又不是ASP控件,偶然找到這段代碼,特收藏。2009-07-07
C#數(shù)據(jù)綁定控件中的DataSource屬性淺談
使用該屬性指定用來(lái)填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對(duì)象, 如用于訪問數(shù)據(jù)庫(kù)的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對(duì)象2013-02-02
使用Ajax更新ASP.Net MVC項(xiàng)目中的報(bào)表對(duì)象方法
下面小編就為大家分享一篇使用Ajax更新ASP.Net MVC項(xiàng)目中的報(bào)表對(duì)象方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-01-01
asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁(yè)的方法
這篇文章主要介紹了asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁(yè)的方法,比較簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2014-08-08

