简体   繁体   English

ASP.NET服务器控件属性问题(?)

[英]ASP.NET server control property issue(?)

I am working on some ASP.NET Server Control, and I have an issue. 我正在使用某些ASP.NET Server Control,但遇到了问题。 Maybe I oversee something, I don't know. 也许我监督了一些事情,我不知道。

Anyway: 无论如何:

public string Name
{
    get
    {
        String s = (String)ViewState["name"];
        return ((s == null) ? String.Empty : s);
    }

    set
    {
        ViewState["name"] = value;
    }
}



protected override void RenderContents(HtmlTextWriter output)
{
    txt.ID = Name; // Name here exists
    txt.Text = DateTime.Now.ToShortDateString();

    txt.RenderControl(output);

    output.Write(someName(someValue));

}

public string GetCalendarString(string date)
{
    some code...
    // Name property is null
}

'RenderContents' uses property 'Name' to set the control name and then calls 'someName' function and 'someName' function also uses property 'Name', but when I run it, property 'Name' inside function 'someName' is empty, although in 'RenderContents' it is not. 'RenderContents'使用属性'Name'设置控件名称,然后调用'someName'函数,并且'someName'函数也使用属性'Name',但是当我运行它时,函数'someName'中的属性'Name'为空,尽管在“ RenderContents”中却不是。

Gremlins, or I'm missing something? 小妖精,还是我想念什么?

This is going to be tough to answer because without debugging there will be no way to tell what outside forces have manipulated the data. 这将很难回答,因为如果不进行调试,将无法告诉外界什么因素操纵了数据。

Since Name is a public property any control that has access to it could set it to null at any point prior to your call to GetCalendarString . 由于Name是公共属性,因此任何有权访问它的控件都可以在调用GetCalendarString之前的任何时候将其设置为null Also if you are calling GetCalendarString before ViewState is loaded this value will not be available. 同样,如果在加载ViewState之前调用GetCalendarString ,则此值将不可用。 I suspect that you are trying to get Name from ViewState before it has loaded but again this is a problem best solved by debugging. 怀疑您正在尝试从ViewState获取Name ,然后再加载它,但这又是通过调试最好解决的问题。

By the way - here is a great image that shows the ASP.NET life cycle and will help you figure out if you are trying to use ViewState before it is loaded from the Request . 顺便说一句- 这是一张展示ASP.NET生命周期的出色图像 ,它将帮助您弄清楚是否要在从Request加载ViewState之前使用ViewState

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

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