Hi

Can Anyone help me in creating custom controls to add or remove textboxes dynamically. Any help is appreciated

Thanks

Manju

Recommended Answers

All 6 Replies

Hi

Can Anyone help me in creating custom controls to add or remove textboxes dynamically. Any help is appreciated

Thanks

Manju

hi.. u can do this by placing the required control on the page and by default set its visibility to false..to get that control on page as per requirement use any event of the control on the basis of which u need that control dynamically or the way you need the control available.

>Can Anyone help me in creating custom controls to add or remove textboxes dynamically.

Show us your code please. Do you know how to add controls dynamically onto the page?

Don't know how to add it dynamically. Can I get a sample code or an example of it?

Thanks

Manju

Can Anyone help me in creating custom controls to add or remove textboxes dynamically.

Because of ASP.NET's view state functionality, this can be a tricky thing to do. If you add all your textboxes during the OnInit, and maintain that they all have the same names as before the postback, then you'll be able to add to them, and even give the new ones values. Here is a sample from an Itinerary builder I made as a final project.

int myRowCount;
	private TextBox[] items;
	private DropDownList[] hour;
	private DropDownList[] min;
	private DropDownList[] AMPM;
	private TableRow[] rows;
	private TableCell[] cells1;
	private TableCell[] cells2;
	DBHandler myDbHandler;
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		myRowCount = (int)Session["RowCount"];
		if (!IsPostBack)
		{
			string EventID = Request.QueryString["EventID"];
			if (string.IsNullOrEmpty(EventID))
			{
				Response.Redirect("EventManagement.aspx");
			}
			myDbHandler = (DBHandler)Application["myDBHandler"];
			if (myDbHandler.GetItineraryItems(EventID).Rows.Count > 1)
			{
				//if (!myDbHandler.DeleteItineraryItems(EventID))
				//{
				//}
			}
			lblTitle.Text = myDbHandler.GetIntineraryTitle(EventID,false,false);
			lblLocation.Text = myDbHandler.GetItineraryLocation(EventID,false);
			lblDate.Text = myDbHandler.GetEventDate(EventID);
		}
		if (IsPostBack)
		{
			myDbHandler = (DBHandler)Application["myDBHandler"];
			if (myRowCount == 1)
			{
				myRowCount++;
			}
		}

		items = new TextBox[myRowCount];
		rows = new TableRow[myRowCount];
		hour = new DropDownList[myRowCount];
		min = new DropDownList[myRowCount];
		AMPM = new DropDownList[myRowCount];
		rows = new TableRow[myRowCount];
		cells1 = new TableCell[myRowCount];
		cells2 = new TableCell[myRowCount];
		for (int i = 0; i < myRowCount; i++)
		{
			TableRow row = new TableRow();
			TableCell cell1 = new TableCell();
			TableCell cell2 = new TableCell();
			DropDownList ddlHour = new DropDownList();
			DropDownList ddlMin = new DropDownList();
			DropDownList ddlAMPM = new DropDownList();
			Label rowNumber = new Label();
			TextBox item = new TextBox();
			Button newRow = new Button();
			Button delRow = new Button();
			newRow.ID = "btnNewRow" + i.ToString();
			newRow.Click += new EventHandler(btnAddRow_Click);
			newRow.Enabled = false;
			newRow.Text = "+";
			if (i == myRowCount - 1)
			{
				newRow.Enabled = true;
			}
			delRow.ID = "btnDelRow" + i.ToString();
			delRow.Click += new EventHandler(btnDelRow_Click);
			delRow.Enabled = false;
			delRow.Text = "+";
			if (i == myRowCount - 1)
			{
				delRow.Enabled = true;
			}
			rowNumber.Text = (i + 1).ToString("00");
			ddlHour.ID = "ddlHour" + i.ToString();
			ddlHour.Items.Add("");
			for (int j = 1; j <= 12; j++)
			{
				ddlHour.Items.Add(j.ToString());
			}
			hour[i] = ddlHour;
			ddlMin.ID = "ddlMin" + i.ToString();
			ddlMin.Items.Add("");
			for (int j = 0; j <= 55; j = j + 5)
			{
				ddlMin.Items.Add(j.ToString("00"));
			}
			min[i] = ddlMin;
			ddlAMPM.ID = "ddlAMPM" + i.ToString();
			ddlAMPM.Items.Add("AM");
			ddlAMPM.Items.Add("PM");
			AMPM[i] = ddlAMPM;
			item.ID = "txtItem" + i.ToString();
			items[i] = item;
			cell2.Controls.Add(item);
			cell2.Controls.Add(newRow);
			cell1.Controls.Add(rowNumber);
			cell1.Controls.Add(ddlHour);
			cell1.Controls.Add(ddlMin);
			cell1.Controls.Add(ddlAMPM);
			cells2[i] = cell2;
			cells1[i] = cell1;
			row.Cells.Add(cell1);
			row.Cells.Add(cell2);
			rows[i] = row;
			tblItinItems.Rows.Add(row);
		}
		//myPlaceholder.Controls.Add(myTable); 

	}

I forgot to add that I had a button event that incremented the counters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.