[急]访问自定义控件中公开属性问题
public class LoginControl : Control
{
private TextBox userName;
public String UserName
{
get
{
return userName.Text;
}
set
{
userName.Text = value;
}
}
protected override void CreateChildControls()
{
userName = new TextBox();
userName.ID = "UserName";
userName.MaxLength = 16;
this.Controls.Add(userName);
}
}
<test:LoginControl id="ctlLogin" runat="server"/>
我在后台页面的Page_Load方法中访问ctlLogin.UserName属性,提示未将对象引用到对象的实例。
protected LoginControl ctlLogin;
public void Page_Load(Object sender, EventArgs e)
{
Response.Write(ctlLogin.UserName);//提示未引用对象实例;
}
然后给控件添加了个构造函数
public void LoginControl()
{
userName = new TextBox();
}
然后可以访问了,但是页面提交后使用Response.Write(ctlLogin.UserName)得不到值。该怎么做?