How to add an item to the list of database data SqlDataSource

I'm lazy - and I'm using SQLDataSource to populate my dropdownLists.

The Databind event for data-bound objects is raised in front of the .PreRender page, so I am doing something like this in the PreRender eventHandler:

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

I know that I can add AppendDataBound elements to true and hardcode my custom element in the markup, but before returning to this, I would like to understand why what I'm doing is not working.

This usually works when I bind things dynamically as follows:

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

What I do should be equivalent - the only difference is that I use SQLDataSource.

Any help appreciated!

+2
2

AppendDataBoundItems="true" dropDwonList charme! , . .

+4

Source: https://habr.com/ru/post/1712221/


All Articles