关闭

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

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

 看完pethsop4中的web/controls/Addressform.ascx之后,才发现对这篇文章理解有误,其实可以用控件来完成表单,而这个控件跟一个实体类一般是一对一,且属性相同,所有可以把这个实体类作为控件的属性,把控件的输入的控件的内容就等于这个实体类的属性。Addressform.ascx.cs代码如下:

 

using PetShop.Model;
using System.Text.RegularExpressions;

namespace PetShop.Web {

    
public partial class AddressForm : System.Web.UI.UserControl {
                        
        
/// <summary>
        
/// Control property to set or get the address
        
/// </summary>
        public AddressInfo Address {
            
get {

                
// Return null if control is empty
                if (string.IsNullOrEmpty(txtFirstName.Text) && 
                   
string.IsNullOrEmpty(txtLastName.Text) && 
                   
string.IsNullOrEmpty(txtAddress1.Text) && 
                   
string.IsNullOrEmpty(txtAddress2.Text) && 
                   
string.IsNullOrEmpty(txtCity.Text) && 
                   
string.IsNullOrEmpty(txtZip.Text) && 
                   
string.IsNullOrEmpty(txtEmail.Text) && 
                   
string.IsNullOrEmpty(txtPhone.Text))
                    
return null;

                
// Make sure we clean the input
                string firstName = WebUtility.InputText(txtFirstName.Text, 50);
                
string lastName = WebUtility.InputText(txtLastName.Text, 50);
                
string address1 = WebUtility.InputText(txtAddress1.Text, 50);
                
string address2 = WebUtility.InputText(txtAddress2.Text, 50);
                
string city = WebUtility.InputText(txtCity.Text, 50);
                
string zip = WebUtility.InputText(txtZip.Text, 10);
                
string phone = WebUtility.InputText(WebUtility.CleanNonWord(txtPhone.Text), 10);
                
string email = WebUtility.InputText(txtEmail.Text, 80);          
                
string state = WebUtility.InputText(listState.SelectedItem.Value, 2);
                
string country = WebUtility.InputText(listCountry.SelectedItem.Value, 50);
                
                
return new AddressInfo(firstName, lastName, address1, address2, city, state, zip, country, phone, email);
            }
            
set {
                
if(value != null) {
                    
if(!string.IsNullOrEmpty(value.FirstName))
                        txtFirstName.Text 
= value.FirstName;
                    
if(!string.IsNullOrEmpty(value.LastName))
                        txtLastName.Text 
= value.LastName;
                    
if(!string.IsNullOrEmpty(value.Address1))
                        txtAddress1.Text 
= value.Address1;
                    
if(!string.IsNullOrEmpty(value.Address2))
                        txtAddress2.Text 
= value.Address2;
                    
if(!string.IsNullOrEmpty(value.City))
                        txtCity.Text 
= value.City;
                    
if(!string.IsNullOrEmpty(value.Zip))
                        txtZip.Text 
= value.Zip;
                    
if(!string.IsNullOrEmpty(value.Phone))
                        txtPhone.Text 
= value.Phone;
                    
if(!string.IsNullOrEmpty(value.Email))
                        txtEmail.Text 
= value.Email;
                    
if(!string.IsNullOrEmpty(value.State)) {
                        listState.ClearSelection();
                        listState.SelectedValue 
= value.State;
                    }
                    
if(!string.IsNullOrEmpty(value.Country)) {
                        listCountry.ClearSelection();
                        listCountry.SelectedValue 
= value.Country;
                    }
                }
            } 
        }

    }
}

 

参考:

1.运用反射给实体赋值:http://dotnet.e800.com.cn/articles/2007/11/1167639881603150378_1.html 

2.面向对象开发中的数据控件绑定方案http://www.cchensoft.com/Articles/ArticleDetails.aspx?id=fzp2nm

 

0
0

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