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

将页面与实体关联的方法:我也写了一个

编辑控件前台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

阅读更多
个人分类:
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭