- 热门文章:
- · 动态加载Asp.net分页控件
- · ASP.Net Error Pocessing method
- · 正则表达式Replace
- · 正则表达式
- · 探讨一下分布式结构在网络应用中的种种好处
- · ASP.NET 2.0中隐藏listbox的某一项
- · ASP.NET下的Page Controller以及Template Method
- · ASP.NET 2.0中隐藏listbox的某一项
- · NET中打印包含有格式的 RichTextBox 的内容
- · 创建ASP.NET WEB自定义控件——例程1
- · 创建ASP.NET WEB自定义控件——例程1
- · VB6如何让程序只能启动一个实例
上一篇:ASP.NET中使用自定义验证控件 >>
三层结构ASP.NET程序中,把实体类自动显示在页面上的例子(c#)
这个实体类的数据显示在一个StudentInfo.aspx页面上,StudentInfo.aspx中有两个文本框:StudentName(用来显示Student.Name)
StudentAge(用来显示Student.Age).
下面的步骤将通过反射和Attribute来实现自动把Student实体显示在StudentInfo中:
1,首先,先需要实现一个Attribute用来表明实体类中的字段与界面中的控件的对应关系。
using System;
using System.Reflection
public class ControlIDAttribute:Attribute
{
public string ID;
public ControlIDAttribute(string id)
{
ID=id;
}
}
2,然后,需要在实体类中给字段绑上ControlID
using System;
public class Student
{
[ControlID("StudentName")]
public string name;
[ControlID("StudentAge")]
public int age;
public Class1(){}
}
3,实现一个工具类来完成实体类显示到界面上的工作
public class PageUtility
{
//遍历页面,绑定数据
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;
}
}
下一篇:动态加载Asp.net分页控件 >>
相关文章:
- · VB6如何让程序只能启动一个实例
- · 在ASP.NET中动态修改文件下载
- · 在asp.net中使用SQLSERVER的高级用法
- · ASP.NET心得笔记
- · [ASP.NET]如何在客户端调用服务端代码
- · ASP.NET POST方式提交数据
- · ASP.NET GET 方式提交数据!
- · ASP.NET的include的用法
- · asp。net关于三层连接数据库
- · .NET中的设计模式四:命令模式
- · VS.NET安装指南(To菜鸟)
- · .net中实现运行时从字符串动态创建对象
- · [ASP.NET]按键跳转以及按Enter以不同参数提交,及其他感应事件
- · [ASP.NET]一个实用的弹出窗口函数
- · [ASP.NET]实现在执行完服务端代码后弹出提醒对话框
- · 在 ASP.NET 中实现会话状态的基础
- · 如何解决ASP.NET中中文不能正常显示问题?
- · 把aspx文件编译成DLL文件
- · Asp.net连Access时:操作必须使用一个可更新的查询
- · ASP.NET中的应用程序配置
- · 基于ASP.NET的网页复用方法
- · 在ASP.NET访问Excel文件
- · Asp.net中DataGrid控件的自定义分页
- · 将ASP.NET Control转换为String
- · 在ASP.NET中杀死进程
- · 在ASP.NET中将数据直接输出成Excel内容
- · 在ASP.NET里轻松实现缩略图
- · 用ASP.NET加密Cookie数据
- · 在ASP.NET中实现多文件上传
- · 在ASP.NET中动态创建柱状图和饼图
- · 利用ASP.NET DataGrid显示主次关系的数据
- · 常用ASP。NET技巧
- · 采用HttpModules来重写URLs(实践篇)
- · 在 ASP.NET 中执行 URL 重写
- · 利用 ASP.NET 2.0 创建自定义 Web 控件
- · ASP.NET应用程序的安全模型
- · DVNEWS 3.2 1013版免虚拟目录的安装方法,只要三个步骤
- · ASP.NET十大技巧
