简体   繁体   English

asp.net Web控件的公共属性保存在哪里?

[英]Where is the public properties of a asp.net web control saved?

I have a web control that derives from another web control (in this case GridView). 我有一个从另一个Web控件(在本例中为GridView)派生的Web控件。 When I add new properties to that web control, such as 当我向该Web控件添加新属性时,例如

public string name {get;set;} 

Is that property being saved somewhere? 该财产被保存在某个地方吗? If so, is it stored in Control State automatically or do I need to override the SaveControlState function ... 如果是这样,它是自动存储在控件状态中还是我需要重写SaveControlState函数?

I was able to determine the behavior of the application, but would like to know the correct implementation. 我能够确定应用程序的行为,但想知道正确的实现。

There are some other ways, but out of scope for this question, see below: 还有其他方法,但是超出此问题的范围,请参见下文:

//stores it in memory
public string name {get;set;} 

//viewstate backed property, preserved on postback when viewstate is enabled
public string name {
    get
    {
        return (string)ViewState["name "] ?? String.Empty; //default value
    }
    set
    {
        ViewState["name "] = value;
    }
}

Properties declared in that way are backed by a private field that's created by the compiler. 以这种方式声明的属性由编译器创建的私有字段支持。 They only persist for the life of the class. 他们只坚持一生。

If you want to persist between postbacks, use ViewState as a backing store. 如果要在两次回发之间保持不变,请使用ViewState作为后备存储。 You could also, as you suggest, override the control state methods, but ViewState is probably simpler. 如您所建议,您也可以覆盖控件状态方法,但是ViewState可能更简单。

ETA: Mr. Schott in his reply has a good example of using ViewState. ETA:Schott先生在答复中有使用ViewState的一个很好的例子。

What do you mean by "Is that property being saved somewhere?" “该财产是否保存在某个地方?”是什么意思? If you add the property to your class, then it's a member of your class and is included on the heap in the scope of an instance of that class. 如果将属性添加到类中,则它是类的成员,并且包含在该类实例的作用域中的堆中。

Are you just asking about the syntax of Auto-Implemented Properties ? 您只是在询问自动实现属性的语法吗? When you write this: 当您编写此代码时:

public string name {get;set;}

It's really just syntactic sugar. 它实际上只是语法糖。 By the time the code is being executed, the compiler has turned it into something more like this: 在执行代码时,编译器已将其转换为以下形式:

private string _name;
public string name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
    }
}

Auto-Implemented Properties are just a way to reduce the amount of code you need to type, allowing you to focus on what you're trying to express without having to wrap it in so much language overhead that doesn't really contribute to the expressiveness of the code. 自动实现的属性只是一个方式,以减少的代码,你需要输入量,让你专注于你想表达,而不必把它包在这么多语言的开销并没有真正有助于表现是什么代码。 The end result is exactly the same as implementing the entire property manually, it's just a shortcut if you don't need to add any custom logic to the property. 最终结果与手动实现整个属性完全相同,如果您不需要向该属性添加任何自定义逻辑,那只是捷径。

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

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