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

时间:2022-09-24 23:50:27

I use model view presenter approach for ASP.NET web site. The presentation part is compiled in separate class library where view contracts and presenters are defined:

我使用模型视图演示程序来实现ASP。网的网站。演示部分是在单独的类库中编译的,在这里,视图契约和presenter被定义为:

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. By all means if you replace the only line in myButton click event with direct setting of myLabel Text property everything stays assigned.

然而,在退出了Presenter.DisplayText()文本属性之后,myLabel的文本属性再次变为null,就像没有赋值一样。无论如何,如果您替换myButton单击事件中的唯一一行,直接设置myLabel文本属性,那么一切都将被分配。

1 个解决方案

#1


1  

Does the Generate method return a non-null string? 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.

要诊断,请传递一个文本字符串来查看。显示文本而不是调用生成。

#1


1  

Does the Generate method return a non-null string? 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.

要诊断,请传递一个文本字符串来查看。显示文本而不是调用生成。