3

I am new in asp.net. I am trying to a static list pass into my view page.

Following code I have written on my _Default class:

protected void Page_Load(object sender, EventArgs e)
{
    List<String> itemlist = new List<string>();
    itemlist.Add("Drink water");
    itemlist.Add("Sleep more");
    itemlist.Add("Drink tea");
    itemlist.Add("Drink water");
    itemlist.Add("Exercise more");
    itemlist.Add("Eat healthier");
    itemlist.Add("Smile");
    itemlist.Add("Do Yoga");
}

Now I am trying to pass above list into my Default.aspx page but I am not sure how to do that.

Please let me know how I can pass and display above list into my Default.aspx with HTML controls.

1
  • How do you want to show them actually? In a select box, in an ordered list? Commented Nov 1, 2016 at 13:54

4 Answers 4

4

At first wrap your list with a function then call it from anywhere it's needed.

  public List<String> MyToDOList()
    {

        List<String> itemlist = new List<string>();
        itemlist.Add("Drink water");
        itemlist.Add("Sleep more");
        itemlist.Add("Drink tea");
        itemlist.Add("Drink water");
        itemlist.Add("Exercise more");
        itemlist.Add("Eat healthier");
        itemlist.Add("Smile");
        itemlist.Add("Do Yoga");

        return itemlist;
    }

Followings are sample code if you would like to display your lists with html tag on your .aspx file:

<%
    var todo_list=MyToDOList();
    Response.Write("<ul>");
    foreach(var item in todo_list)
    {
        Response.Write("<li>"+item+"</li><br/>");
    }
    Response.Write("</ul>");
    %>
Sign up to request clarification or add additional context in comments.

Comments

2

In your aspx file (view)

<asp:DropDownList id="ddlItems" runat="server" AutoPostBack="True"></asp:DropDownList>

In your aspx.cs file(code)

protected void Page_Load(object sender, EventArgs e)
 {
    List<String> itemlist = new List<string>();
    itemlist.Add("Drink water");
    itemlist.Add("Sleep more");
    itemlist.Add("Drink tea");
    itemlist.Add("Drink water");
    itemlist.Add("Exercise more");
    itemlist.Add("Eat healthier");
    itemlist.Add("Smile");
    itemlist.Add("Do Yoga");

    ddlItems.DataSource = itemlist;
    ddlItems.DataBind();
}

Comments

0

Add a DropDownList to your aspx page. Then add the following lines to your Page_Load:

 this.DropDownList1.DataSource = itemlist;
 DropDownList1.DataBind();

Comments

0

If you want to show the list into a specific control (like DropDownList or Gridview) you can just pass the list to its DataSource and then call DataBind method. But if you want just show items you can also use ASP:Repeater control or just write the content using Response.write method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.