简体   繁体   English

从类库程序集设置ASP.NET页面控件

[英]Setting ASP.NET page controls from class library assembly

I use model view presenter approach for ASP.NET web site. 我对ASP.NET网站使用模型视图演示器方法。 The presentation part is compiled in separate class library where view contracts and presenters are defined: 演示文稿部分在单独的类库中编译,其中定义了视图合同和演示者:

MyPresentation assembly
IMyView
{
     DisplayText(string text);
}
MyPresenter
{
     public IMyView View { get; set; }
     public DisplayText()
     {
         string text = Generate();
         View.DisplayText(text);   
     } 
}

MyWebApplication assembly
public partial class MyForm : System.Web.UI.Page, IMyView
{
    public void DisplayText(string text)
    {
        this.myLabel.Text = text;  
    }
    ...
    protected void myButton_Click(object sender, EventArgs e) 
    {
        Presenter.DisplayText(); // calls this.DisplayText()     
    }        
}

However after stepping out of Presenter.DisplayText() Text property of myLabel becomes null again as though no assignment were done. 但是,退出Presenter.DisplayText()myLabel Text属性再次变为null,好像没有完成分配。 By all means if you replace the only line in myButton click event with direct setting of myLabel Text property everything stays assigned. 如果您用myLabel Text属性的直接设置替换了myButton click事件中的唯一行,则一切将保持分配状态。

Does the Generate method return a non-null string? Generate方法是否返回非空字符串? That looks like about the only culprit in this case. 在这种情况下,这似乎是唯一的罪魁祸首。 It might also explain why setting myLabel directly works. 它也可以解释为什么设置myLabel直接起作用。

To diagnose, pass a literal string to View.DisplayText instead of calling Generate . 为了进行诊断, View.DisplayText文字字符串传递给View.DisplayText而不是调用Generate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM