Inserting Item into ListItem collection for bound control

Posts   
 
    
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 20-Sep-2006 16:24:46   

I have a bound combo box in a web user control. The web user control contains the DataSource2 control. I bind the combo box to the DataSource2 control. LivePersistence is turned off and I am handling PerformSelect.

After the control is bound, I need to insert an empty item into the combo box. What is the best practice for doing this? I tried putting this code in place but it didnt work.


protected void CountryDataSource_PerformSelect(object sender, PerformSelectEventArgs2 e)
    {
        LoadCountries(e);
        CountryCombo.Items.Insert(0, new ListItem(string.Empty, string.Empty));
    }

With regards to page / control event lifecycle, where does the PerformSelect fall?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Sep-2006 16:35:04   

After databinding, just use this:

CountryCombo.Items.Insert(0, " ");

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 20-Sep-2006 16:59:43   

Walaa wrote:

After databinding, just use this:

CountryCombo.Items.Insert(0, " ");

Moved the code to here:


    protected void CountryCombo_DataBound(object sender, EventArgs e)
    {
        CountryCombo.Items.Insert(0, "");
        CountryCombo.SelectedIndex = 0;
    }

And it worked. Thanks for the help.