A Collection in a DropDownList

Posts   
 
    
vdroos
User
Posts: 3
Joined: 24-Nov-2004
# Posted on: 07-Dec-2004 15:13:54   

Hi!

How can I easily show a collection in a DropDownList in a Webform?

this is what I have now, but I don't know how I can import the data from a collection:

 <asp:dropdownlist id="FunctieDD" style="Z-INDEX: 108; LEFT: 176px; POSITION: absolute; TOP: 176px" DataSource = ????  runat="server" Width="168px" DataValueField="FunctieID" DataTextField="Functie" ></asp:dropdownlist>

THNX

Vincent

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 07-Dec-2004 15:33:54   

I just did this yesterday. This is from my Page_Load(). Note that I wanted to add an initial item [Select] to encourage the user to actually select something.


                ListItem lItem = new ListItem("[Select]", "-1");
                DepartmentsCollection dc = new DepartmentsCollection();
                dc.GetMulti(null);
                DepartmentList.DataSource = dc;
                DepartmentList.DataValueField = "DepartmentID";
                DepartmentList.DataTextField = "Department";
                DepartmentList.DataBind();
                DepartmentList.Items.Insert(0, lItem);


I think you can also bind it by following the instructions in the manual to add your collection to the toolbox in VS and then dragging it to the form, etc. Hope this helps.

Jim

vdroos
User
Posts: 3
Joined: 24-Nov-2004
# Posted on: 07-Dec-2004 15:54:23   

Yes, I got it, it works. But I want to load the data in the HTML code, and not in the code behind. Is that also possible?

I had made think I need do something with the DataSourse = ????

 <asp:dropdownlist id="FunctieDD" style="Z-INDEX: 108; LEFT: 176px; POSITION: absolute; TOP: 176px" DataSource = ???? runat="server" Width="168px" DataValueField="FunctieID" DataTextField="Functie" ></asp:dropdownlist>

But thnx anyway!

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 07-Dec-2004 16:57:30   

Anything you can do in code-behind you should be able to do directly inline in the aspx page, as I understand it. I think the code-behind model is a lot easier to maintain, but I'll let someone else take stab at doing the way you want.