Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a DropDownList from an array of classes 3

Status
Not open for further replies.

grande

Programmer
Feb 14, 2005
657
CA
I'm trying to populate an ASP:DropDownList on PageLoad using a C# script. I would like the DropDownList to be created using an array of objects, but it's not working and I'm at a loss for how to proceed.

Here's the code for the dropdownlist in sc2.aspx
Code:
<asp:DropDownList ID="ddlSellingCompanies" runat="server" />

Here's the code for the script in sc2.aspx.cs
Code:
protected void Page_Load(object sender, EventArgs e)
{
    getCompanies();
}

private string getCompanies()
{
    //Here, I get an array of SellingCompanies called companies

    if (companies != null)
    {
        ddlSellingCompanies.DataSource = companies;
        ddlSellingCompanies.DataTextField = "Name";
        ddlSellingCompanies.DataValueField = "ID";
    }

    return content;
}

And finally, here is some of the appropriate code from my SellingCompany class.
Code:
public string ID
{
    get { return _ID; }
    set { _ID = value; }
}

public string Name
{
    get { return _name; }
    set { _name = value; }
}

Can you guys make any suggestions?

PS - If I've made any really obvious, newbie mistakes, I apologise. I've only been working with C# for about a week, and this is my first dive into ASP.

-------------------------
Just call me Captain Awesome.
 
For debugging purposes, you might want to display the number of companies that are in your array. Then you will know if the problem is with your data retrieval or with your data binding to that control.

If it's the data binding, why not be stupid about it and loop through each item in the array and add it to the dropdown? :)
 
1) Good thought. I went through and ensured that the array is properly done, and completely filled with data. The problem is still there though.

2) I was planning to do that, actually. But there doesn't seem to be a DropDownList.Add method (or anything similar).

-------------------------
Just call me Captain Awesome.
 
DropDownList ddl = new DropDownList();
ddl.Items.Add(new ListItem("Text To Show", "The Value"));

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Thanks Guru!

-------------------------
Just call me Captain Awesome.
 
NO problem.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
your original code needs [tt]ddlSellingCompanies.DataBind();[/tt]

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
D'oh! That is a good call, jmeckley.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Arg! Thanks, JMeckley

-------------------------
Just call me Captain Awesome.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top