DropDownList的SelectedValue对SelectedItem.Value [英] SelectedValue vs SelectedItem.Value of DropDownList

查看:209
本文介绍了DropDownList的SelectedValue对SelectedItem.Value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个旧的项目,多年来被几个人打补丁。在某些地方,他们使用SelectedValue属性和其他使用SelectedItem.Value的地方。



问题: SelectedValue 只是 SelectedItem.Value SelectedValue 的语法糖引擎盖下不同?哪一个表现更好?



编辑: SelectedItem.Text已被SelectedItem.Value替换

解决方案

SelectedValue 返回与 SelectedItem.Value 相同的值。 / p>

SelectedItem.Value SelectedItem.Text 可能有不同价值观和表现不是一个因素,只有这些属性的含义很重要。

 < asp:DropDownList runat =服务器ID =ddlUserTypes> 
< asp:ListItem Text =AdminsValue =1Selected =true/>
< asp:ListItem Text =UsersValue =2/>
< / asp:DropDownList>

这里, ddlUserTypes.SelectedItem.Value == ddlUserTypes.SelectedValue 并且都将返回值1。



ddlUserTypes.SelectedItem.Text 将返回管理员,这不同于 ddlUserTypes.SelectedValue



编辑



在引擎盖下,SelectedValue看起来像这样

  public virtual string SelectedValue 
{
get
{
int selectedIndex = this.SelectedIndex;
if(selectedIndex> = 0)
{
return this.Items [selectedIndex] .Value;
}
return string.Empty;
}
}

和SelectedItem如下所示:

  public virtual ListItem SelectedItem 
{
get
{
int selectedIndex = this.SelectedIndex;
if(selectedIndex> = 0)
{
return this.Items [selectedIndex];
}
返回null;
}
}

这两个属性之间的一个主要区别是, code> SelectedValue 还有一个 setter ,因为 SelectedItem 没有。编写代码时, SelectedValue getter 更快,执行性能的问题没有真正的理由被讨论。 SelectedValue的一大优点是使用Binding表达式时。



编辑数据绑定场景(您不能使用SelectedItem.Value) p>

 < asp:Repeater runat =server> 
< ItemTemplate>
< asp:DropDownList ID =ddlCategoriesrunat =server
SelectedValue ='<%#Eval(CategoryId)'>
< / asp:DropDownList>
< / ItemTemplate>
< / asp:Repeater>


I'm working on an old project written and then patched by several people over the years. At some places they have used SelectedValue property and other places they used SelectedItem.Value.

Question: Is SelectedValue just a syntactic sugar for SelectedItem.Value or SelectedValue works differently under the hood? Which one performs better?

Edit: SelectedItem.Text was replaced with SelectedItem.Value

解决方案

SelectedValue returns the same value as SelectedItem.Value.

SelectedItem.Value and SelectedItem.Text might have different values and the performance is not a factor here, only the meanings of these properties matters.

<asp:DropDownList runat="server" ID="ddlUserTypes">
    <asp:ListItem Text="Admins" Value="1" Selected="true" />
    <asp:ListItem Text="Users" Value="2"/>
</asp:DropDownList>

Here, ddlUserTypes.SelectedItem.Value == ddlUserTypes.SelectedValue and both would return the value "1".

ddlUserTypes.SelectedItem.Text would return "Admins", which is different from ddlUserTypes.SelectedValue

edit

under the hood, SelectedValue looks like this

public virtual string SelectedValue
{
    get
    {
        int selectedIndex = this.SelectedIndex;
        if (selectedIndex >= 0)
        {
            return this.Items[selectedIndex].Value;
        }
        return string.Empty;
    }
}

and SelectedItem looks like this:

public virtual ListItem SelectedItem
{
    get
    {
        int selectedIndex = this.SelectedIndex;
        if (selectedIndex >= 0)
        {
            return this.Items[selectedIndex];
        }
        return null;
    }
}

One major difference between these two properties is that the SelectedValue has a setter also, since SelectedItem doesn't. The getter of SelectedValue is faster when writing code, and the problem of execution performance has no real reason to be discussed. Also a big advantage of SelectedValue is when using Binding expressions.

edit data binding scenario (you can't use SelectedItem.Value)

<asp:Repeater runat="server">
 <ItemTemplate>
     <asp:DropDownList ID="ddlCategories" runat="server" 
                       SelectedValue='<%# Eval("CategoryId")'>
     </asp:DropDownList>
 </ItemTemplate>
</asp:Repeater>

这篇关于DropDownList的SelectedValue对SelectedItem.Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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