您的位置:首页 > 编程语言 > ASP

读使用反射将业务对象绑定到 ASP.NET 窗体控件有感

2007-09-13 10:45 543 查看
将页面与实体关联的方法:我也写了一个

编辑控件前台edit.ascx


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="edit.ascx.cs" Inherits="edit" %>


<asp:Label ID="Label1" runat="server" Text="编号"></asp:Label>


<asp:TextBox ID="txtID" runat="server"></asp:TextBox>


<br />


<asp:Label ID="Label2" runat="server" Text="姓名"></asp:Label>


<asp:TextBox ID="txtName" runat="server"></asp:TextBox>


<br />


<asp:Label ID="Label3" runat="server" Text="密码"></asp:Label>


<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>


<br />


<asp:Button ID="btnOp" runat="server" Text="操作" />


<asp:Button ID="btnBack" runat="server" Text="返回" />



后台代码edit.ascx.cs


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;




public partial class edit : System.Web.UI.UserControl




...{


public int UserId




...{


set




...{


this.txtID.Text = value.ToString();


}


get




...{


if (this.txtID.Text != "")


return int.Parse(this.txtID.Text);


return 0;


}


}


public string UserName




...{


set




...{


this.txtName.Text = value;


}


get




...{


return this.txtName.Text;


}


}


public string Password




...{


set




...{


this.txtPwd.Text = value.ToString();


}


get




...{


return this.txtPwd.Text;


}


}


protected void Page_Load(object sender, EventArgs e)




...{


this.btnBack.Attributes.Add("onclick", "history.go(-1);");


}


}



前台显示的后台代码eidt.aspx.cs


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;




using System.Reflection;




public partial class edit : System.Web.UI.Page




...{


protected void Page_Load(object sender, EventArgs e)




...{




User user=new User(95,"test","test");


PropertyInfo[] objProperties = user.GetType().GetProperties();


PropertyInfo[] ctlProperties = this.Edit1.GetType().GetProperties();
//public static object CopyProperty(object obj1, object obj2)
//{
// Type souType = user.GetType();
// Type tarType = this.Edit1.GetType();
// PropertyInfo[] pis = souType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// if (null != pis)
// {
// foreach (PropertyInfo pi in pis)
// {
// string propertyName = pi.Name;
// PropertyInfo pit = tarType.GetProperty(propertyName);
// if (pit != null)
// {
// pit.SetValue(obj2, pi.GetValue(obj1, null), null);
// }
// }
// }
// return obj2;
//}以下代码可被以上注释的代码替换


foreach (PropertyInfo objProperty in objProperties)




...{


foreach (PropertyInfo ctlProperty in ctlProperties)




...{


if (objProperty.Name == ctlProperty.Name)




...{


ctlProperty.SetValue(this.Edit1, objProperty.GetValue(user, null), null);
break;


}


}


}


}


}



前台eidt.aspx代码


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="edit.aspx.cs" Inherits="edit" Debug="true" %>




<%@ Register Src="edit.ascx" TagName="edit" TagPrefix="uc1" %>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title>Untitled Page</title>


</head>


<body>


<form id="form1" runat="server">


<div>


<uc1:edit id="Edit1" runat="server">


</uc1:edit></div>


</form>


</body>


</html>



实体类代码:


using System;


using System.Data;


using System.Configuration;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;






/**//// <summary>


/// Summary description for User


/// </summary>


public class User




...{




"Fields and Properties"#region "Fields and Properties"


private int userId;


private string userName;


private string password;


public int UserId




...{




get ...{ return userId; }




set ...{ userId = value; }


}


public string UserName




...{




get ...{ return userName; }




set ...{ userName = value; }


}


public string Password




...{




get ...{ return password; }




set ...{ password = value; }


}


#endregion




"Constructors"#region "Constructors"




public User() ...{ }


public User(int id, string name, string password)




...{


this.UserId = id;


this.UserName = name;


this.Password = password;


}


#endregion


}



以上理解失误:请见http://blog.csdn.net/beimuaihui/archive/2007/09/14/1785035.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: