Can the value and text properties of a dropdown list be used independently?

前端 未结 2 1718
遥遥无期
遥遥无期 2021-01-21 21:46

Can these two properties of a dropdown list in ASP.NET be used independently?

I wanted to retrieve a null value when the user selects some text; I couldn\'t

相关标签:
2条回答
  • 2021-01-21 22:11

    Just set the value to some sentinel value like an empty string or some crazy string "JANDKJASD_" and handle it accordingly.

    0 讨论(0)
  • 2021-01-21 22:21

    Your observation is correct. Contrary to what intuition tells us, ListControl.Text (and, thus, DropDownList.Text) does not return the Text property of the currently selected ListItem. Here's an excerpt from the documentation:

    ListControl.Text Property

    Gets or sets the SelectedValue property of the ListControl control.

    [...]

    Remarks

    The Text property gets and sets the same value that the SelectedValue property does.

    To get the Text property of the selected ListItem, use SelectedItem to retrieve the currently selected list item and then access the Text property.

    So, the behavior you are seeing is by design. Why did the .NET developers specify ListControl.Text in such an unintuitive way? I have no idea. Maybe it was necessary to support the ITextControl interface...

    0 讨论(0)
提交回复
热议问题