[英]How can I connect up my UserControl properties in such a way that they retain state on postback?
In an ASP.NET Webforms application I have a submission form containing a UserControl with properties as such: 在ASP.NET Webforms应用程序中,我有一个提交表单,其中包含具有以下属性的UserControl:
public string Name
{
get
{
String s = (String)ViewState["Name"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Name"] = value;
}
}
This is adapted from an MSDN walk-through. 这是从MSDN演练改编而来的。 This is assumed to be "bound" (not databinding as I don't think that's possible) to the contents of a text box defined in the ascx as below:
假定这是对ascx中定义的文本框内容的“绑定”(不是数据绑定,因为我认为这是不可能的),如下所示:
<asp:TextBox runat="server" ID="name" />
The question is, what is the best practice for allowing this to be accessible as a public property, and retain state on postback? 问题是,允许将此作为公共财产进行访问并在回发时保留状态的最佳实践是什么?
The simplest option is to have the public properties on your UserControl
delegate to the properties of the child controls: 最简单的选择是将
UserControl
委托上的公共属性分配给子控件的属性:
public string Name
{
get { return name.Text; }
set { name.Text = value; }
}
The TextBox
will then take care of maintaining the state on postback. 然后,
TextBox
将负责维护回发状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.