4

I have a DropDownList that is associated with a DataSource in the aspx page. I need to add one more item when the page is loaded.

My Code:

<asp:LabelDropDownList ID="ddlVisualTemplate" runat="server" LabelText="Visual Template:"      DataSourceID="VisualTemplateDataSource" DataTextField="Name" DataValueField="Id" AutoPostBack="true" OnSelectedIndexChanged="ddlVisualTemplate_SelectedIndexChanged"/>                         

<asp:EntityDataSource ID="VisualTemplateDataSource" runat="server" 
     ConnectionString="name=Entities" 
     DefaultContainerName="Entities" EnableFlattening="False" 
     EntitySetName="tbEmailVisualTemplates">

And I am trying to an extra item to it:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlVisualTemplate.Items.Add(new ListItem("None", string.Empty));
        }
    }

If I debug the code, it goes through it. But When the page is displayed dropdown doesn't contain "None".

1
  • You should do that, but after te data is rendered, on the DataBound Event Jun 30, 2011 at 22:17

5 Answers 5

15

Probably too late for the original poster, but maybe useful for other users:

You can add the value "None", "Choose value", etc. in the designer (or in the code) and prevent DataBind from overwriting it, by setting AppendDataBoundItems="true". This will make DataBind append rather than clear.

Below example from Scott Guthrie's post ListControl.AppendDataBoundItems Property in ASP.NET 2.0.

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">    
    <asp:ListItem Text="(Select a State)" Value="" />   
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
                   ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
                   SelectCommand="SELECT DISTINCT [state] FROM [authors]">
</asp:SqlDataSource>
12

It's most probably because you're adding the item before the DataBind(). If you want to add an item with

ddlVisualTemplate.Items.Add()

then you have to do it after the dropdown is being bound.

If you look at http://msdn.microsoft.com/en-us/library/ms178472.aspx then DataBind is being done in PreRenderComplete. So you have to add the element in some event that occurs after PreRenderComplete. Or you could do it on the ddlVisualTemplate.DataBound event.

0
6

You could easily fix this by setting the datasource prgrammatically:

ddlVisualTemplate.DataSource = VisualTemplateDataSource;
ddlVisualTemplate.DataBind();

ddlVisualTemplate.Items.Add(new ListItem("None", string.Empty));

BTW, these datasource controls are a wrong thing in asp.net in my opinion. I don't like the idea of defining the data source and giving the control over db connections to the aspx page. For a better way of doing this just google about session per request pattern, separation of concerns and n-tier apps.

Update: instead of "VisualTemplateDataSource" you could call directly the data. I don't know EF, but it might be like this: "DataContext.tbEmailVisualTemplates". You have to set your datacontext. And then you can get rid of the datasource control.

0
1

Use this to add an item in the bound dropdown list at 0 index

ddlTicketType.Items.Insert(0, new ListItem("All", "0"));
0

This fixed it for me but it puts the value in the end.

protected void ddlTest_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlTest.Items.Add(new ListItem("All", string.Empty));
    }
}
1
  • 2
    ddlTest.Items.Insert(0, new ListItem("All", string.Empty)); Will put the item at the top
    – MoMo
    May 30, 2013 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.