如何添加项目到SqlDataSource的数据绑定列表绑定、项目、数据、列表

2023-09-04 23:25:51 作者:想陪你走过好些个春夏秋冬

我懒 - 和我使用一个SqlDataSource来填充我的dropdownLists

I am lazy - and I am using a SQLDataSource to populate my dropdownLists.

在数据绑定事件的数据绑定对象的页面之前被调用,所以我做这样的事情在preRender事件处理程序preRender:

The Databind event for the databound objects is called before the Page.PreRender so I am doing something like this in the PreRender eventHandler:

private void InitializeDropDown()
        {
            this.myDropDown.Items.Insert(0, new ListItem("-- Select something --"));
        }

我知道我可以设置AppendDataBound项目真正的硬code在标记我自定义项,但恢复到之前,我想知道为什么我在做什么是不工作。

I know I can set AppendDataBound items to true and hardcode my custom item in the markup but before reverting to that I'd like to understand why what I am doing is not working.

它通常当我动态绑定的东西是这样工作的:

It usually works when I bind stuff dynamically like this:

myDropDown.DataTextField = "whatever";
myDropDown.DataValueField = "ID";
myDropDown.DataSource = GetStuff();
myDropDown.DataBind();
myDropDown.Items.Insert(0, "-- Select something --");

我在做什么应该是等价的 - 唯一的区别是我使用一个SqlDataSource

What I am doing should be equivalent - only difference is I am using a SQLDataSource.

任何帮助AP preciated!

Any help appreciated!

推荐答案

我设置 AppendDataBoundItems =真正的的dropDwonList,它的工作原理是风情万种! 我认为这是应该仅用于在标记硬codeD列表项presence。我错了。

I set AppendDataBoundItems="true" for the dropDwonList and it works like charme! I thought it was supposed to be used only in presence of hardcoded list items in the markup. I was wrong.