可以在下拉列表中的值和文本属性可以独立使用? [英] Can the value and text properties of a dropdown list be used independently?

查看:80
本文介绍了可以在下拉列表中的值和文本属性可以独立使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否在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 the Text property whenever the Value is null. 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 the Text 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 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...

这篇关于可以在下拉列表中的值和文本属性可以独立使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆