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

翻译|其它|编辑:郝浩|2007-09-13 11:52:02.000|阅读 994 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

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

编辑控件前台  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();
         
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
}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP