Append items to drop down lists

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have a drop down list ddlServers and in my data access layer I run a
stored procedure to populate it. I need to be able to append items to the
drop down list and don't know how to do this. Here is the code I use to bind
the data retrieved from the DAL to the field.

{
DAL GetAllServers = new DAL();
DataSet dsServerNames = GetAllServers.GetAllServers();
ddlServers.DataSource = dsServerNames;
ddlServers.DataTextField = "Server_Name";
ddlServers.DataValueField = "Server_Id";
ddlServers.DataBind();
ddlServers.Items.Insert(0,new ListItem("--Select a Server--",""));
Console.WriteLine();
}
 
Samantha,

If I remember correctly, when you make a call to DataBind, it will
render the control, so your call to insert a new item will not be rendered.

Rather, you should take your data set, and add the item to that before
the call to DataBind. Either that, or populate the items manually from the
data set, adding your item at the end (or beginning).

Hope this helps.
 
Thank you so much for addressing this!


Nicholas Paldino said:
Samantha,

If I remember correctly, when you make a call to DataBind, it will
render the control, so your call to insert a new item will not be rendered.

Rather, you should take your data set, and add the item to that before
the call to DataBind. Either that, or populate the items manually from the
data set, adding your item at the end (or beginning).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Samantha Penhale said:
Hi, I have a drop down list ddlServers and in my data access layer I run a
stored procedure to populate it. I need to be able to append items to the
drop down list and don't know how to do this. Here is the code I use to
bind
the data retrieved from the DAL to the field.

{
DAL GetAllServers = new DAL();
DataSet dsServerNames = GetAllServers.GetAllServers();
ddlServers.DataSource = dsServerNames;
ddlServers.DataTextField = "Server_Name";
ddlServers.DataValueField = "Server_Id";
ddlServers.DataBind();
ddlServers.Items.Insert(0,new ListItem("--Select a Server--",""));
Console.WriteLine();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top