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

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

问题描述

我正在开发一个旧项目,多年来由几个人编写并修补.在某些地方他们使用了 SelectedValue 属性,而在其他地方他们使用了 SelectedItem.Value.

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.

问题:SelectedValue 只是SelectedItem.ValueSelectedValue语法糖代码> 引擎盖下的工作方式不同?哪个表现更好?

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

SelectedItem.Text 被替换为 SelectedItem.Value

SelectedItem.Text was replaced with SelectedItem.Value

推荐答案

SelectedValue 返回与 SelectedItem.Value 相同的值.

SelectedValue returns the same value as SelectedItem.Value.

SelectedItem.ValueSelectedItem.Text 可能有不同的值,性能在这里不是一个因素,只有这些属性的含义很重要.

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>

这里,ddlUserTypes.SelectedItem.Value == ddlUserTypes.SelectedValue 并且两者都会返回值1".

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

ddlUserTypes.SelectedItem.Text 将返回Admins",这与 ddlUserTypes.SelectedValue

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

编辑

在幕后,SelectedValue 看起来像这样

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;
    }
}

和 SelectedItem 看起来像这样:

and SelectedItem looks like this:

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

这两个属性之间的一个主要区别是 SelectedValue 也有一个 setter,因为 SelectedItem 没有.SelectedValuegetter在写代码的时候速度更快,执行性能的问题没有真正值得讨论的理由.SelectedValue 的另一个重要优势是在使用绑定表达式时.

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.

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

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>

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

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