Reputation: 295
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:
The array created: [To,[email protected], orange]
Array or data table: [To,[email protected], orange] -- Row 1 [Cc,[email protected], Red] -- Row 2
Upvotes: 1
Views: 790
Reputation: 353
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
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
Upvotes: 0
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