Use an array to populate a listbox?

The website I am working on will contain a drop-down list that shows a number of reason codes.

Is it possible to populate a drop-down list with an array derived from a custom class?

Something like this:

ASPX Page

<asp:DropDownList ID="ddlReasonsWhy" runat="server"></asp:DropDownList>

ASPX Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    //instantiate custom class
    Class1 reasonsList = new Class1();
   //populate reasons list
   this.ddlReasonsWhy.Items.Add(reasonsList.getReasons);

Custom Class

public string[] getReasons()
    {
        string[] Reasons;
        Reasons[0] = "test";
        Reasons[1] = "test2";
        Reasons[2] = "test3";

        return Reasons;
    }

UPDATE1:

Thanks to the answers and this stack link my final solution is similar to Ahmet and silky answers.

Class

 public List<ListItem> getRejectReasons()
    {
        List<ListItem> reasons = new List<ListItem>();
        reasons.Add(new ListItem("Select Value", "0"));
        reasons.Add(new ListItem("Red", "1"));
        reasons.Add(new ListItem("Green", "2"));
        return reasons;

    }

Code Behind

 //instantiate custom class
 Class1 reject = new Class1();
 this.ddlReasonsWhy.DataSource = reject.getRejectReasons();
 this.ddlReasonsWhy.DataBind();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use ListItem instead of string and Addrange for Add

edit: getReasons is not a variable so use getReasons()

ASPXPAGE

<asp:DropDownList ID="ddlReasonsWhy" runat="server"></asp:DropDownList>

ASPX Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    //instantiate custom class
    Class1 reasonsList = new Class1();
   //populate reasons list
   this.ddlReasonsWhy.Items.AddRange(reasonsList.getReasons());
}

Custom Class

public ListItem[] getReasons()
    {
        ListItem[] Reasons;
        Reasons[0] = "test";
        Reasons[1] = "test2";
        Reasons[2] = "test3";

        return Reasons;
    }

Method 2

— Edit:

I’ve noticed you specifically wanted to bind to an array. I don’t believe that is possible (may be wrong); I leave my example below of how to do it for a custom class, perhaps it is of use, perhaps not. Hopefully someone else answers you more directly.

— Old:

Certainly it is, like so:

ddlList.DataSource     = yourDataSource;
ddlList.DataTextField  = "DisplayProperty";
ddlList.DataValueField = "PropertyForValue";
ddlList.DataBind();

But note, in your example you’ve not posted the class, you’ve posted a method. In the example about ‘yourDataSource’ should be something like:

List<YourObjects> yourDataSource = new List<YourObjects>();

Method 3

(untested)

this.ddlReasonsWhy.DataSource = reasonsList.getReasons;
this.ddlReasonsWhy.DataBind();

Method 4

Just a thought, but how about:

this.ddlReasonsWhy.Items.Add(reasonsList.getReasons().Select(r => new ListItem(r)));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x