看ms的petshop3.0遇到的问题,下面是源码:
public abstract class AddressUI : System.Web.UI.UserControl {
//...
//...
//... // Page property to set or get the address
public AddressInfo Address {
get {
// Make sure we clean the input
string firstName = WebComponents.CleanString.InputText(txtFirstName.Text, 50);
string lastName = WebComponents.CleanString.InputText(txtLastName.Text, 50);
string address1 = WebComponents.CleanString.InputText(txtAddress1.Text, 50);
string address2 = WebComponents.CleanString.InputText(txtAddress2.Text, 50);
string city = WebComponents.CleanString.InputText(txtCity.Text, 50);
string state = WebComponents.CleanString.InputText(listState.SelectedItem.Text, 2);
string zip = WebComponents.CleanString.InputText(txtZip.Text, 10);
string country = WebComponents.CleanString.InputText(listCountry.SelectedItem.Text, 50);
string phone = WebComponents.CleanString.InputText(txtPhone.Text, 10); return new AddressInfo(firstName, lastName, address1, address2, city, state, zip, country, phone);
}
set {
txtFirstName.Text = value.FirstName;
txtLastName.Text = value.LastName;
txtAddress1.Text = value.Address1;
txtAddress2.Text = value.Address2;
txtCity.Text = value.City;
txtZip.Text = value.Zip;
txtPhone.Text = value.Phone;
listState.SelectedItem.Value = value.State;
listCountry.SelectedItem.Value = value.Country;
}
}
下面是访问抽象类AddressUI的非抽象属性的源码:
protected AddressUI addr;
AddressInfo address = addr.Address;下面是我的疑问:
addr不是只是一个声明吗?并没有分配内存空间啊,为什么可以访问到属性Address呢?
public abstract class AddressUI : System.Web.UI.UserControl {
//...
//...
//... // Page property to set or get the address
public AddressInfo Address {
get {
// Make sure we clean the input
string firstName = WebComponents.CleanString.InputText(txtFirstName.Text, 50);
string lastName = WebComponents.CleanString.InputText(txtLastName.Text, 50);
string address1 = WebComponents.CleanString.InputText(txtAddress1.Text, 50);
string address2 = WebComponents.CleanString.InputText(txtAddress2.Text, 50);
string city = WebComponents.CleanString.InputText(txtCity.Text, 50);
string state = WebComponents.CleanString.InputText(listState.SelectedItem.Text, 2);
string zip = WebComponents.CleanString.InputText(txtZip.Text, 10);
string country = WebComponents.CleanString.InputText(listCountry.SelectedItem.Text, 50);
string phone = WebComponents.CleanString.InputText(txtPhone.Text, 10); return new AddressInfo(firstName, lastName, address1, address2, city, state, zip, country, phone);
}
set {
txtFirstName.Text = value.FirstName;
txtLastName.Text = value.LastName;
txtAddress1.Text = value.Address1;
txtAddress2.Text = value.Address2;
txtCity.Text = value.City;
txtZip.Text = value.Zip;
txtPhone.Text = value.Phone;
listState.SelectedItem.Value = value.State;
listCountry.SelectedItem.Value = value.Country;
}
}
下面是访问抽象类AddressUI的非抽象属性的源码:
protected AddressUI addr;
AddressInfo address = addr.Address;下面是我的疑问:
addr不是只是一个声明吗?并没有分配内存空间啊,为什么可以访问到属性Address呢?
解决方案 »
- 未能加载文件或程序集
- ASP.NET指定前台事件执行JS方法错误
- 网站在我本机正常.. 上传到空间上就打不开了 为什么啊...
- Socket编程高手进来
- 如何下载文件
- asp.net下怎样做这样的二维表格,而且还要AJAX?
- 请问include file的路径及文件名能否动态给予?
- selectionlist返回选定数据问题??急
- 如何在datagrid的删除按钮点击后弹出确认对话框,基于web的!这个问题就没人能解决么?
- 命名空间名称“SQL”在类或命名空间“System.Data”中不存在如何解决?
- DOT NET研讨群,群号16450496希望对DOT NET有一定开发经验的朋友加入,拒绝新手
- 关于DropDownList无刷新和用js验证服务器端控件<asp:textbox>的数值是否为数字 的问题
using PetShop.Model;
using PetShop.BLL;
using PetShop.Web.Controls;
//using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;namespace PetShop.Web {
public class CreateAccount : Page { // Assume a duplicate record is the reason for the error
private const string MSG_FAILURE = "Duplicate user ID! Please try again."; protected TextBox txtUserId;
protected TextBox txtPassword;
protected TextBox txtEmail;
protected RequiredFieldValidator valUserId;
protected AddressUI addr;
protected NavBar header;
protected System.Web.UI.WebControls.RequiredFieldValidator valPassword;
protected System.Web.UI.WebControls.RequiredFieldValidator valEmail;
protected System.Web.UI.WebControls.ImageButton btnSubmit;
protected System.Web.UI.HtmlControls.HtmlForm frmCreateAcct;
protected Preferences prefs; protected void SubmitClicked(object sender, ImageClickEventArgs e) {
if (Page.IsValid) { string userId = WebComponents.CleanString.InputText(txtUserId.Text, 50);
string password = WebComponents.CleanString.InputText(txtPassword.Text, 50);
string email = WebComponents.CleanString.InputText(txtEmail.Text, 50);
AddressInfo address = addr.Address;
string language = prefs.Language;
string favCategory = prefs.Category;
bool showFavorites = prefs.IsShowFavorites;
bool showBanners = prefs.IsShowBanners; // Store all the customers information in an account business entity
AccountInfo accountInfo = new AccountInfo(userId, password, email, address, language, favCategory, showFavorites, showBanners); ProcessFlow.AccountController accountController = new ProcessFlow.AccountController(); if (!accountController.CreateAccount(accountInfo)){ // Tell the user they have failed to create an account
valUserId.ErrorMessage = MSG_FAILURE;
valUserId.IsValid = false;
}
}
}
}
}
private const string MSG_FAILURE = "Duplicate user ID! Please try again."; protected TextBox txtUserId;
protected TextBox txtPassword;
protected TextBox txtEmail;
protected RequiredFieldValidator valUserId;
protected AddressUI addr;
//CreateAccount.aspx 会在被第一次访问是被编译为一个继承于 CreateAccount 类的子类,在
那个子类里会实例华AddressUi 实例
我大概明白你的意思,AddressUi和普通的服务器控件是相同的处理方式
但是AddressUi被声明为:public abstract class AddressUI : System.Web.UI.UserControl
为什么抽象类可以实例化呢??
实在还有点不解.
照这样说,ms为什么要用抽象类呢,用具体类不可以了?水平太菜,困扰啊...
public abstract class AddressUI : System.Web.UI.UserControl
是个用户控件
在CreateAccount.aspx类里会实例华AddressUI对应的AddressUI.ascx类,并不是AddressUI类
而类AddressUI.ascx类是AddressUI的子类,
所以和abstract 类AddressUI 不能实例化并没有冲突,是我一开始想不明白
有提高了一步,嘿嘿,顶者有分...