web层的控件之二AddressForm

这个控件是用来记录注册用户的资料的。本来这个好像没有什么可记录的,但他的处理方式可谓是高人一等。所以在此记录下来。下面这张是这个控件实现的功能:

重点 看它是怎样收集这些信息的,要是我的话估计要定义很多string了,看下他们是怎样处理的吧:

View Code
 1 namespace PetShop.Web {
2
3 publicpartialclass AddressForm : System.Web.UI.UserControl {
4
5 ///<summary>
6 /// Control property to set or get the address
7 ///</summary>
8 public AddressInfo Address {
9 get {
10
11 // Return null if control is empty
12 if (string.IsNullOrEmpty(txtFirstName.Text) &&
13 string.IsNullOrEmpty(txtLastName.Text) &&
14 string.IsNullOrEmpty(txtAddress1.Text) &&
15 string.IsNullOrEmpty(txtAddress2.Text) &&
16 string.IsNullOrEmpty(txtCity.Text) &&
17 string.IsNullOrEmpty(txtZip.Text) &&
18 string.IsNullOrEmpty(txtEmail.Text) &&
19 string.IsNullOrEmpty(txtPhone.Text))
20 returnnull;
21
22 // Make sure we clean the input
23 //用到了WebUtility里的InputText方法对输入的字符串进行限制
24 string firstName = WebUtility.InputText(txtFirstName.Text, 50);
25 string lastName = WebUtility.InputText(txtLastName.Text, 50);
26 string address1 = WebUtility.InputText(txtAddress1.Text, 50);
27 string address2 = WebUtility.InputText(txtAddress2.Text, 50);
28 string city = WebUtility.InputText(txtCity.Text, 50);
29 string zip = WebUtility.InputText(txtZip.Text, 10);
30 string phone = WebUtility.InputText(WebUtility.CleanNonWord(txtPhone.Text), 10);
31 string email = WebUtility.InputText(txtEmail.Text, 80);
32 string state = WebUtility.InputText(listState.SelectedItem.Value, 2);
33 string country = WebUtility.InputText(listCountry.SelectedItem.Value, 50);
34
35 returnnew AddressInfo(firstName, lastName, address1, address2, city, state, zip, country, phone, email);
36 }
37 set {
38 if(value !=null) {
39 if(!string.IsNullOrEmpty(value.FirstName))
40 txtFirstName.Text = value.FirstName;
41 if(!string.IsNullOrEmpty(value.LastName))
42 txtLastName.Text = value.LastName;
43 if(!string.IsNullOrEmpty(value.Address1))
44 txtAddress1.Text = value.Address1;
45 if(!string.IsNullOrEmpty(value.Address2))
46 txtAddress2.Text = value.Address2;
47 if(!string.IsNullOrEmpty(value.City))
48 txtCity.Text = value.City;
49 if(!string.IsNullOrEmpty(value.Zip))
50 txtZip.Text = value.Zip;
51 if(!string.IsNullOrEmpty(value.Phone))
52 txtPhone.Text = value.Phone;
53 if(!string.IsNullOrEmpty(value.Email))
54 txtEmail.Text = value.Email;
55 if(!string.IsNullOrEmpty(value.State)) {
56 listState.ClearSelection();
57 listState.SelectedValue = value.State;
58 }
59 if(!string.IsNullOrEmpty(value.Country)) {
60 listCountry.ClearSelection();
61 listCountry.SelectedValue = value.Country;
62 }
63 }
64 }
65 }
66
67 }
68 }

他们的每个控件都放在同一个命名空间中---PetShop.Web。

下面来看看这个WebUtility到底实现了哪些业务吧(这里我就只摘下跟这个控件有关的代码):

publicstaticstring InputText(string text, int maxLength) {
text
= text.Trim();//去掉文本的空格
if (string.IsNullOrEmpty(text))//如果文本为空
returnstring.Empty;//返回空
if (text.Length > maxLength)//如果文本宽大于maxLength(应该是限制的长度)
text = text.Substring(0, maxLength);//截掉大于maxLength的部分
text = Regex.Replace(text, "[\\s]{2,}", ""); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", ""); //&nbsp;
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
}

上面我有注释了。但具体怎么调用呢?具体是这样的,在 CheckOut.aspx用到了这个控件,所以呢,在加载的时调用 了:

protectedvoid Page_Load(object sender, EventArgs e) {
if (billingForm.Address ==null) {
billingForm.Address
= Profile.AccountInfo; //成员账户资料
}
}

真是长见识了,原来也可以这样做。

原文地址:https://www.cnblogs.com/huaizuo/p/2108177.html