DROPDOWNLIST第一个值显示"选择" [英] Dropdownlist first value display "choose"

查看:262
本文介绍了DROPDOWNLIST第一个值显示"选择"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的DropDownList显示第一个值 - 选择汽车 -

I want that my dropdownlist display first value: "-choose car-"

我在这样成功的:

protected void ddl1_DataBound(object sender, EventArgs e)  
{
    Convert.ToInt32(ddl1.SelectedValue); 
    ddl1.Items.Insert(0, new ListItem("-Choose car-", "-Choose car-"  )); 
}

和这没关系,在请选择 - 是摆在首位,但现在的问题是,如果我有值,例如,将DropDownList显示这样的:

and that's ok,the "-choose-" is in the first place but the problem now is that if I have values,for example,the dropdownlist show like that:

-Choose car-
Subaro
Fiat
Honda

第一个值,当我进入到现场的是Subaro,并看到 - 选择CAR-用户需要打开DropDownList的,然后他会看到 - 选择CAR-在首位的显示。我怎么能做到这一点从一开始,从页面加载 - ? - 选择在CAR-会显示在从页load.Where的DDL我错在code

The first value that display when I'm enter to the site is the Subaro,and to see the -choose car- the user need to open the dropdownlist and then he will see the -choose car- at the first place.How can I do that from the start,from the page load - the -choose car- will display at the ddl from the page load.Where I wrong at the code ?

我试图与ITEMLIST AppendDataBoundItems =真正的,但我得到一个错误,当我成功了,这个问题是一样的像我之前说的。

I tried the itemlist with AppendDataBoundItems = "true" but I got an error, and when I succeed,the problem is the same like I said before.

推荐答案

您在正确的轨道上与使用 AppendDataBoundItems 财产,应设置为真正如果你绑定列表

You were on the right track with using the AppendDataBoundItems property, it should be set to true if you're databinding the list.

您的标记应该是这样的。

Your markup should look like this

<asp:DropDownList runat="server" ID="ddl1" AppendDataBoundItems="true">
    <asp:ListItem Text="-Choose car-" />
</asp:DropDownList>

和背后的code可能已经看起来像这样

and your code behind probably already looks something like this

ddl1.DataSource = [your datasource goes here];
ddl1.DataBind();

这将会把选择汽车文本作为下拉列表中的第一个选项,选项的其余部分追加它下面。

This will place the Choose car text as the first option in the drop-down list and append the rest of the options below it.

现在的更有趣的部分的为什么你看你看(没有被选择的第一项)的行为。如果你看的<一实施href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedindex.aspx\"相对=nofollow> 的SelectedIndex 使用的工具,像的 JustDecompile (不与Telerik的关联,只是碰巧使用他们的工具),你会看到code,看起来像这样:

Now for the more interesting part of why you were seeing the behavior you were seeing (first item not being selected). If you look at the implementation of SelectedIndex using a tool like JustDecompile (not affiliated with Telerik, just happen to use their tool) you'll see code that looks like this:

public int SelectedIndex
{
    get
    {
        int num = 0;
        num++;
        while (num < this.Items.Count)
        {
            if (this.Items[num].Selected)
            {
                return num;
            }
        }
        return -1;
    }
    set
    {
        // stuff you don't care about

        this.ClearSelection();
        if (value >= 0)
        {
            this.Items[value].Selected = true;
        }

        // more stuff you don't care about
    }
}

正如你可以看到,指数不存储,它的计算基础,每次上项具有属性设置为true。当您在标记和数据绑定您的数据源设置的SelectedIndex 0时,它会选择该列表中的第0项,你的情况 Subaro 。当你在列表的开头插入一个新的项目, Subaro 仍标记为选定的项目,这就是为什么在页面加载时,您将看到所选的,而不是选择汽车。如果你想标记选择汽车作为使用code所选的项目,你将不得不这样做后的数据进行数据绑定的下拉列表。 请注意,这只是一个如何 DROPDOWNLIST 和实现细节。它可以在ASP.NET的未来版本中改变,所以不要写code依赖于它的工作这种方式。

As you can see, the index isn't stored anywhere, it's computed every time based on which item has the Selected property set to true. When you set the SelectedIndex to 0 in the markup and databind your datasource, it will select the 0th item in that list, in your case Subaro. When you insert a new item at the beginning of the list, Subaro is still marked as the selected item, which is why when the page loads, you see that selected and not Choose car. If you want to mark Choose car as the selected item using code, you will have to do it after you data databind your dropdown. Please note, this is just an implementation detail of how DropdownList works. It could change in future version of ASP.NET so do not write code that relies on it working this way.

这篇关于DROPDOWNLIST第一个值显示&QUOT;选择&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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