不,它可以是一個很好的解決方案,以使用屬性來設置和獲取控制值(不包括專門爲僅HtmlControls)。在使用Model View Presenter (MVP) pattern時,我多次使用它們。
這裏是MVP圖案
模型的一個示例:
public interface IPresentationModel
{
string AProperty { set; }
string ATextbox { get; set; }
event EventHandler SaveData;
event EventHandler LoadData;
}
視圖,標記:
<form id="form1" runat="server">
<div>
<span id="aSpan" runat="server"></span>
<input type="text" id="aTextbox" runat="server"/>
<asp:Button ID="ButtonSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
</div>
</form>
查看,代碼隱藏
public partial class View : Page, IPresentationModel
{
private Presenter _presenter;
protected override void OnInitComplete(EventArgs e)
{
base.OnInitComplete(e);
_presenter = new Presenter(this);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData(this, e);
}
}
public string AProperty
{
set
{
aSpan.InnerHtml = value;
}
}
public string ATextbox
{
get
{
return aTextbox.Value;
}
set
{
aTextbox.Value = value;
}
}
public event EventHandler SaveData;
public event EventHandler LoadData;
protected void ButtonSave_Click(object sender, EventArgs e)
{
SaveData(this, e);
LoadData(this, e);
}
}
演示
public class Presenter
{
private readonly IPresentationModel _model;
private static string _fakeService = "42";
public Presenter(IPresentationModel model)
{
_model = model;
_model.SaveData += SaveData;
_model.LoadData += LoadData;
}
private void LoadData(object sender, EventArgs e)
{
_model.AProperty = _fakeService;
_model.ATextbox = _fakeService;
}
private void SaveData(object sender, EventArgs e)
{
_fakeService = _model.ATextbox;
}
}