---
title: "Sending out two textboxes as opposed to one dynamically"  
description: "Sending out two textboxes as opposed to one dynamically"  
author: "Manoj Bhatt"  
published: 2014-08-28  
updated: 2014-08-28  
canonical: https://www.mindstick.com/forum/2186/sending-out-two-textboxes-as-opposed-to-one-dynamically  
category: "c#"  
tags: ["c#", ".net", "dynamically", "opposed", "Sending out two textboxes as opposed"]  
reading_time: 1 minute  

---

# Sending out two textboxes as opposed to one dynamically

public [partial class](https://www.mindstick.com/forum/155717/define-partial-class-in-mvc) [Testing](https://www.mindstick.com/articles/1849/role-of-testing-in-software-development) : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

// Add any [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) that have been previously added dynamically

for (int i = 0; i < TotalNumberAdded; ++i)

{

AddControls(i + 1);

}

// Attach the [event handler](https://www.mindstick.com/forum/1371/c-sharp-dot-net-event-handler-delegate-question) to the button

Button1.Click += new EventHandler(Button1_Click);

}

protected void Button1_Click(object sender, EventArgs e)

{

// [Increase](https://www.mindstick.com/articles/12959/are-you-struggling-to-increase-the-profit-of-your-liquor-store) the number added and add the new label and [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview)

TotalNumberAdded++;

AddControls(TotalNumberAdded);

}

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) void AddControls(int controlNumber)

{

var newPanel = new Panel();

var newLabel = new Label();

var newTextbox = new TextBox();

// textbox needs a [unique id](https://www.mindstick.com/forum/33873/how-to-get-unique-id-of-alasset-for-identification-in-ios) to maintain state information

newTextbox.ID = "TextBox_" + controlNumber;

newLabel.Text = "Nature Of Accident";

// add the label and textbox to the panel, then add the panel to the form

newPanel.Controls.Add(newLabel);

newPanel.Controls.Add(newTextbox);

form1.Controls.Add(newPanel);

}

protected int TotalNumberAdded

{

get { return (int)([ViewState](https://www.mindstick.com/blog/326/viewstate-in-asp-dot-net)["TotalNumberAdded"] ?? 0); }

set { ViewState["TotalNumberAdded"] = value; }

}

}

## Replies

### Reply by Sumit Kesarwani

Hi Manoj, \

Does your button have an event tied to it already in the asp page?

Button1.Click += new EventHandler(Button1_Click);

That seems like it could give you some problems, especially on reloads.


---

Original Source: https://www.mindstick.com/forum/2186/sending-out-two-textboxes-as-opposed-to-one-dynamically

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
