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

来源:互联网 发布:aws和阿里云 编辑:程序博客网 时间:2024/05/01 10:44

 看完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

 

原创粉丝点击