ListItem.Value overwrites text if not set

If you set the parameter ListItem.Valuevalue before setting its value Text, how Text, and Valuewill be set to the same value. I can get around this, but I just want to know why this is happening? Is it because something “should” should be installed on the screen? And why rewrite when the default is an empty string.

.Net 3.5

ListItem li = new ListItem();
li.Value = "abc"; //Text is now = "abc"
li.Text = "def";
li.Value = "qwe"; //Text remains "def"
+5
source share
1 answer

This is because the property receiver is Textimplemented as follows:

get
{
    if (this.text != null)
    {
        return this.text;
    }
    if (this.value != null)
    {
        return this.value;
    }
    return string.Empty;
}

MSDN:

Text, , list , ListItem. Text null, get accessor Value. Value, , null, String.Empty.

Value :

Value null, get accessor Text. Text, , null, String.Empty .

+7

All Articles