可以在下拉列表中的值和文本属性可以独立使用? [英] Can the value and text properties of a dropdown list be used independently?
问题描述
能否在ASP.NET下拉列表的这两个属性可以独立使用?
我想,当用户选择一些文本检索空
值;我不能因为它检索文本
属性每当值
是空
。例如:
L1 =新的ListItem(猫,NULL);
Console.WriteLine(l1.Value);
的输出是
猫
块引用>在另一种情况下,当这两个属性有不同的字符串,我得到了
值
属性字符串时,我使用文本
属性。例如:12 =新的ListItem(猫,鼠标);
DropDownList的DDL =新的DropDownList();
ddl.Items.Add(LI);
ddl.SelectedIndex = 0;
Console.WriteLine(ddl.SelectedValue);
Console.WriteLine(ddl.Text);的输出是
鼠标结果
鼠标
块引用>解决方案您的观察是正确的。相反的是直觉告诉我们,
ListControl.Text
(因此,DropDownList.Text
)做的不的返回文本
的当前选择的列表项
属性。下面是文档:
ListControl.Text地产
获取或设置列表控件控制的SelectedValue属性。
[...]
备注
Text属性获取和设置相同的值,SelectedValue属性一样。
块引用>要获取选中
列表项
,使用的SelectedItem $的
文本
属性C $ c>来检索当前选中的列表项,然后访问文本
属性。因此,您所看到的现象是由设计。为什么.NET开发人员在这样的非直观的方式指定
ListControl.Text
?我不知道。也许有必要支持ITextControl
接口...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 as it retrieves theText
property whenever theValue
isnull
. Eg:l1 = new ListItem("Cat", null); Console.WriteLine(l1.Value);
The output is
Cat
In another situation, when both the properties have different strings, I get the string in the
Value
property when I use theText
property. Eg:l2 = new ListItem("Cat", "Mouse"); DropDownList ddl = new DropDownList(); ddl.Items.Add(li); ddl.SelectedIndex = 0; Console.WriteLine(ddl.SelectedValue); Console.WriteLine(ddl.Text);
The output is
Mouse
Mouse
解决方案Your observation is correct. Contrary to what intuition tells us,
ListControl.Text
(and, thus,DropDownList.Text
) does not return theText
property of the currently selectedListItem
. 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 selectedListItem
, useSelectedItem
to retrieve the currently selected list item and then access theText
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 theITextControl
interface...这篇关于可以在下拉列表中的值和文本属性可以独立使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!