Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding items to an existing array

Status
Not open for further replies.

mellenburg

Programmer
Aug 27, 2001
77
US
I have a form that is capturing information from a user. In this example, the user is entering a list of first names. The number of first names entered can be 1 to whatever. I have code that will capture the entry and store it in an array. However, I can't capture the second entry without it overwriting the first. How can I let the user keep clicking the submit button and record all the first names they have entered? Here's the code I have currently.

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TravelSystem
{
	public class TAForm : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataList dlTravelers;
		protected System.Web.UI.WebControls.Label lblFirstName;
		protected System.Web.UI.WebControls.TextBox txtFirstName;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvFirstName;
		protected System.Web.UI.WebControls.Label lblLastName;
		protected System.Web.UI.WebControls.TextBox txtLastName;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvLastName;
		protected System.Web.UI.WebControls.Button btnAddTraveler;
		protected System.Data.DataSet dsTravelers;
	
		protected ArrayList arrFirstName = new ArrayList();

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
			{
			}
		}

		public void btnAddTraveler_Click(object sender, System.EventArgs e)
		{
			arrFirstName.Add(txtFirstName.Text.ToString());

			foreach(string strFirstName in arrFirstName)
			{
				Response.Write(strFirstName);
			}

			dlTravelers.DataSource=arrFirstName;
			dlTravelers.DataBind();
		}
	}
}
 
So you want to capture a on a button click. Then on the next button click capture another name? If so then you'll need to store the array in a viewstate, session, or in the cache. Then check for it's existence (it will not exist the first time the page is loaded). If it does exist then load the cached array, add to it, then store it again.

J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top