国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

三層結(jié)構(gòu)ASP.NET程序中,把實(shí)體類自動(dòng)顯示在頁(yè)面上的例子(c#)

2024-07-10 12:56:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在這里我們假設(shè)這樣一個(gè)場(chǎng)景:在一個(gè)三層bs系統(tǒng)(asp.net)中有一個(gè)實(shí)體類student,包括name,age兩個(gè)字段。現(xiàn)在需要把
這個(gè)實(shí)體類的數(shù)據(jù)顯示在一個(gè)studentinfo.aspx頁(yè)面上,studentinfo.aspx中有兩個(gè)文本框:studentname(用來(lái)顯示student.name)
studentage(用來(lái)顯示student.age).
下面的步驟將通過(guò)反射和attribute來(lái)實(shí)現(xiàn)自動(dòng)把student實(shí)體顯示在studentinfo中:
1,首先,先需要實(shí)現(xiàn)一個(gè)attribute用來(lái)表明實(shí)體類中的字段與界面中的控件的對(duì)應(yīng)關(guān)系。
using system;
using system.reflection
public class controlidattribute:attribute
{
public string id;

public controlidattribute(string id)
{
id=id;
}
}
2,然后,需要在實(shí)體類中給字段綁上controlid
using system;
public class student
{
[controlid("studentname")]
public string name;
[controlid("studentage")]
public int age;

public class1(){}
}
3,實(shí)現(xiàn)一個(gè)工具類來(lái)完成實(shí)體類顯示到界面上的工作
public class pageutility
{
//遍歷頁(yè)面,綁定數(shù)據(jù)
public void binddata( control control,object entity)
{
object temp=null;
foreach(control c in control.controls)
{
temp=getvaluefromentity(c.clientid,entity);

if(c is textbox)
{
((textbox)c).text=temp.tostring();
}
if(c.hascontrols())
{
binddata(c,entity);
}
}
}

//獲取controlidattribute為controlid的值
private object getvaluefromentity(string controlid,object entity)
{
type t = entity.gettype();
foreach(fieldinfo f in t.getfields())
{
foreach(attribute attr in attribute.getcustomattributes(f))
{
if(attr is controlidattribute && ((controlidattribute)attr)._id == controlid )
{
return f.getvalue(entity);
}
}
}
return null;
}
}



菜鳥學(xué)堂:
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 云林县| 调兵山市| 海兴县| 阿克苏市| 赫章县| 如东县| 志丹县| 呼和浩特市| 博罗县| 南充市| 宁明县| 保康县| 科尔| 渝北区| 葫芦岛市| 宝鸡市| 右玉县| 浙江省| 常宁市| 石城县| 西吉县| 铁岭县| 永嘉县| 富宁县| 和平区| 平武县| 隆化县| 和硕县| 湘潭县| 英超| 乐安县| 新昌县| 南溪县| 康乐县| 勐海县| 醴陵市| 沙洋县| 玉环县| 阿尔山市| 滨海县| 富裕县|