-1

I have a "person" class with a constructor that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Programming_handin_2___Arrays
{
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Password { get; set; }

    public Person(string name, int age, string password)
    {
        this.Name = name;
        this.Age = age;
        this.Password = password;
    }

    public override string ToString()
    {
        return Name + ", " + Age + ", " + Password;
    }
}
}

I also have an aspx page with a few Textboxes, to add the variables above.

    <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Name:<br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <br />
            Age:<br />
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <br />
            <br />
            Password:<br />
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add person" />
            <br />
            <br />
        </div>
    </form>
</body>
</html>

My goal is to create a new "person" every time someone fills out the form and clicks the button. The "person" should then be displayed on the page. What is the best way to achieve this?

b1sh0p
  • 3
  • 3
  • The last part being to display them on the page? –  Oct 02 '17 at 16:39
  • Explaining request lifetime, how data is persisted in ASP.Net over multiple requests, working with forms and finally adding an element to an array (which by itself non-trivial) is way too brad for single SO question. You may want to narrow down problem you facing now and ask that specific question instead, but make sure to search first for similar questions (right now it can't be even closed as duplicate to give you some hints) – Alexei Levenkov Oct 02 '17 at 16:48
  • @AlexeiLevenkov It could be assumed through educated guess that the OP is wanting to know how to display the containing elements on the page for each person object. –  Oct 02 '17 at 16:51
  • @lxxtacoxxl if you have to guess what OP has problem it means question is unclear. If you confident that you know what post is asking about - suggest edit first and than answer. – Alexei Levenkov Oct 02 '17 at 16:57
  • @lxxtacoxxl, exactly. I want to know how to display each person object. – b1sh0p Oct 02 '17 at 16:58
  • @AlexeiLevenkov I'm not disagreeing with you on that, I just believe that the large collection of questions you posted with your comment where completely irrelevant and sort-of overkill to prove your point. –  Oct 02 '17 at 16:59
  • @b1sh0p Please make sure you update your question to demonstrate that need clearly. –  Oct 02 '17 at 17:00

2 Answers2

0
List<Person> persons;
 protected void Page_Load(object sender, EventArgs e){

  if(!IsPostBack){
     List<Person> persons= new List<Person>();
     Session["personslist"]=persons;
  }else{
     persons= (List<Person>)Session["personslist"];
  }
}

protected void Button1_Click(object sender, EventArgs e){
 persons.Add(new 
 Person(TextBox1.Text,int.parse(TextBox2.Text),TextBox3.Text));
}
0

This question already has multiple duplicates on this site, so I will include their links with this answer as not all answers are the same, and not all answers may be exactly what you are looking for. Please make sure to do your research before posting, as all I had to do was a quick Google search for "dynamically add elements to page c#" and a second one (replacing c# with javascript). My quick Google search resulted in multiple answers that will help you achieve exactly what you are requesting, I believe I used two of these in the past to achieve the same thing you are trying to do.

The idea is pretty straightforward though. Essentially loop through your collection of Person objects and add elements to a designated parent element until your loop completes. The rest comes down to styling which if you're facing issues there you should start a new question for that.

The following stack overflow posts should get you in the right direction:

Dynamically add HTML to ASP.NET page

How to add HTML elements Dynamically (ASP.NET)

Can I dynamically add HTML within a div tag from C# on load event?

Create <div> and append <div> dynamically

HTML: Add elements dynamically using JS

Also feel free to look into this tutorial, and this resource on the createElement function available in JavaScript.