关闭

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

428人阅读 评论(0) 收藏 举报

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

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

以上理解失误:请见http://blog.csdn.net/beimuaihui/archive/2007/09/14/1785035.aspx

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:397156次
    • 积分:6312
    • 等级:
    • 排名:第4021名
    • 原创:196篇
    • 转载:121篇
    • 译文:1篇
    • 评论:55条
    文章分类