Curtis White
Curtis White

Reputation: 6353

Basic server control

I'm looking at server controls for the first time, and I've a question about this code:

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
    get
    {
        String s = (String)ViewState["Text"];
        return ((s == null) ? "[" + this.ID + "]" : s);
    }

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

I do not understand why this control returns the [id] or the text that is set. I do not see how this makes any sense. Is this just for demonstration or is there a reason for returning the id?

Thanks

Upvotes: 1

Views: 60

Answers (3)

Kris van der Mast
Kris van der Mast

Reputation: 16613

If there's nothing been set for the Text property, in ViewState with other words, then this.ID gets returned.

There's not really a meaning for it but it'll show some text in the Property pane of Visual Studio and on the designer.

Upvotes: 0

Greg B
Greg B

Reputation: 14888

It looks like an example that will show the controls ID if the controls .Text property has not been set.

This is a bit of a "debug" procedure to show that the control is actually rendering even though it hasn't got any data set in it's Text property.

Upvotes: 4

Matthew Jones
Matthew Jones

Reputation: 26190

Makes no sense to me. If I'm asking for text, then I expect if there is no text to get either an empty string or null.

Upvotes: 0

Related Questions