• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1258
  • Last Modified:

dropdownlist listitem arraylist

hi there,
I have an arraylist that binds to a dropdownlist.
My problem is that I use the data from the arraylist more than once for different things...
when I use the data for the dropdownlist I need to prefix the options with a Please Select as the first entry within the arraylist.

I have tried the following,

arraylist test = new
while (reader.Read())
    test.Add(reader["name"]));

this gets the data for the dropdownlist..

as the dropdownlist needs the first option to be please select I have used a listitem within the markup template, shown below.  this doesnt render?  i guess the databind overwrites this..??

<asp:DropDownList ID="test" runat="server">
<asp:ListItem>select</asp:ListItem>
</asp:DropDownList>


any suggestions how I can prefix the arraylist when I use the data for the dropdownlist with a listitem? thanks
0
jimbona27
Asked:
jimbona27
  • 5
  • 3
  • 3
2 Solutions
 
surajgupthaCommented:
After u bind the array list to the datasource, try adding a ListItem into the DropDownList.Items.Insertat(0,selectListItem)

This selectListItem is the ListItem witht he text "[Select One]"
0
 
MogalManicCommented:
Change you asp page to use the AppendDataBoundItems property:
  <asp:DropDownList ID="test" runat="server" AppendDataBoundItems='true'>
<asp:ListItem>select</asp:ListItem>
</asp:DropDownList>
0
 
jimbona27Author Commented:
thanks Surajguptha although this doesnt work.  it complains about the insertat does not exist for listitemcollection?

thanks MogalManic, this works great.  can you confirm that this is just adding the listitem before the binding event?  is that it?  and can you control this, can you add after the bind?

thanks, would like to know both answers, many thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
surajgupthaCommented:
DropDownList1.Items.Insert(0,SelectListItem)
where 0 is the index
0
 
MogalManicCommented:
I believe it is BEFORE the binding.  If you want to append AFTER the binding (so the choice is at the bottom of the list), you could manually add the item as surajguptha suggests.
0
 
jimbona27Author Commented:
i'll be able to provide feedback after wednesday as my computer died and my new laptop :) is arriving then..

sorry for the wait..
0
 
jimbona27Author Commented:
thanks
0
 
jimbona27Author Commented:
Surajguptha I have tried the following although I get an error that the Insertat does not exist within the definition??
        test.DataSource = testoptions;
        test.DataBind();
        test.Insertat(0, "test");

0
 
MogalManicCommented:
Did you mean:
  test.DataSource = testoptions
  test.DataBind()
  test.Items.Insert(0, "test")

This will insert "test" in the 1st item in the droplist
0
 
surajgupthaCommented:
It is different for web and windows control. Yes thats right. Insert(0,"Test") is what i meant :)
0
 
jimbona27Author Commented:
ok many thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now