Link to home
Start Free TrialLog in
Avatar of jimbona27
jimbona27Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of surajguptha
surajguptha
Flag of United States of America image

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]"
Change you asp page to use the AppendDataBoundItems property:
  <asp:DropDownList ID="test" runat="server" AppendDataBoundItems='true'>
<asp:ListItem>select</asp:ListItem>
</asp:DropDownList>
Avatar of jimbona27

ASKER

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
DropDownList1.Items.Insert(0,SelectListItem)
where 0 is the index
SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i'll be able to provide feedback after wednesday as my computer died and my new laptop :) is arriving then..

sorry for the wait..
thanks
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");

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is different for web and windows control. Yes thats right. Insert(0,"Test") is what i meant :)
ok many thanks