国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 澜沧| 中超| 富锦市| 湘潭市| 闽清县| 将乐县| 屯门区| 连城县| 大足县| 永仁县| 新泰市| 成都市| 洪洞县| 怀远县| 青岛市| 南安市| 巨鹿县| 西乌| 宝应县| 宿州市| 临江市| 建瓯市| 安溪县| 呈贡县| 大丰市| 紫阳县| 静安区| 观塘区| 界首市| 当雄县| 大洼县| 鲁山县| 定日县| 科技| 怀来县| 渝北区| 泽库县| 县级市| 津市市| 揭西县| 金华市|