Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET: Why does ListControl.Text return the *value* of the selected ListItem rather than the *text*?
    primarykey
    data
    text
    <p>List controls deriving from <code>ListControl</code>, such as <code>DropDownList</code>, <code>ListBox</code> or <code>RadioButtonList</code>, are populated by a list of <code>ListItem</code>s. A <code>ListItem</code> has a <code>Value</code> and a <code>Text</code> property.</p> <p><code>ListControl</code> offers the following methods to access the currently selected item:</p> <ul> <li><code>ListControl.SelectedItem</code> returns the currently selected <code>ListItem</code>,</li> <li><code>ListControl.SelectedValue</code> returns the <code>Value</code> property of the currently selected <code>ListItem</code>.</li> </ul> <p>Now, the interesting thing is:</p> <ul> <li><code>ListControl.Text</code> returns exactly the same value as <code>ListControl.SelectedValue</code>. It does <em>not</em> return <code>SelectedItem.Text</code>, as one might expect.</li> </ul> <p>This is <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.text.aspx" rel="nofollow noreferrer">by design</a>:</p> <blockquote> <p><strong>ListControl.Text Property</strong></p> <p>Gets or sets the SelectedValue property of the ListControl control.</p> <p>[...]</p> <p><strong>Remarks</strong></p> <p>The Text property gets and sets the same value that the SelectedValue property does.</p> </blockquote> <p>This seems counter-intuitive and <a href="https://stackoverflow.com/questions/5232093">confuses people</a>. My question is: Why was it done this way? I can imagine that providing a <code>Text</code> property is necessary for implementing the <code>ITextControl</code> interface, but why on earth would you choose to have it return the <code>Value</code> of the ListItem rather than the <code>Text</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload