roa765
roa765

Reputation: 295

Store textbox data to an array and do this multiple times C# button click

I have a bootstrap form where the user inputs values into three textboxes: 1. Recipient Type (To, CC, Bcc) 2. Email e.g. [email protected] 3. Team Name e.g. Orange

When the user clicks the add button this should store the first set of values in an array. Then I want the user to be able to input values into the same text boxes and click the add button and it stores these values as a second row into the array and has the ability to continue doing this for however many recipients they need.

Once the user has added all recipients they need I will have a button to be able to view all the recipients added in a table/gridview format.

I have managed to get the textbox values into variables on the backend code. However, I am not able to add this to an array multiple times.

   protected void AddRecipient_Click(object sender, EventArgs e)
  {
      RecipientType = Request.Form["recname"];
      Email = Request.Form["emailname"];
      SubTeam = Request.Form["subteamname"];


  }

Example:

  1. Enter 'To'
  2. Enter '[email protected]'
  3. Enter 'Orange'
  4. Press Add button

The array created: [To,[email protected], orange]

  1. Enter 'Cc'
  2. Enter '[email protected]'
  3. Enter 'Red'

Array or data table: [To,[email protected], orange] -- Row 1 [Cc,[email protected], Red] -- Row 2

Upvotes: 1

Views: 790

Answers (3)

Simplest way is to create a class called "Contents"

public class Contents
{
    public string RecipientType;
    public string Email;
    public string SubTeam;
}

Declare a global variable in code behind

List<Contents> Data;

Put the following in pageload.

 if (!Page.IsPostBack)
    {
         Data = new List<Contents>();
         Session["Data"] = Data;
    }
    else
         Data = (List<Contents>) Session["Data"];

In your Add Button event do this.

protected void AddRecipient_Click(object sender, EventArgs e)
{
        var newItem = new Contents
                      {
                         RecipientType = Request.Form["recname"],
                         Email = Request.Form["emailname"],
                         SubTeam = Request.Form["subteamname"]
                      };
        Data.Add(newItem);
        Session["Data"] = Data;
}

Advantage of doing this is, you can actually bind "Data" to your gridview. And then you can implement update, delete very easily.

Upvotes: 2

Mihir Dave
Mihir Dave

Reputation: 4024

According to me, instead of making the frequent call to server this should be handled on the client side.

So steps should be

  1. User enters data
  2. Press Add button This should store an object in the javascript array(E.g dataArray).
  3. A table or grid should display all the entries in dataArray
  4. And when finally user hits submit. that array should go to the server.

Upvotes: 0

evilGenius
evilGenius

Reputation: 1101

maybe it help you

private string[,] mass;
private int index = 0; 
protected void AddRecipient_Click(object sender, EventArgs e)
  {
            mass[index,0] = Request.Form["recname"];
            mass[index,1] = Request.Form["emailname"];
            mass[index,2] = Request.Form["subteamname"];
            index++;


  }

Upvotes: 0

Related Questions