I was faced with the problem in one of my Web projects of retrieving the value of the dynamically created TextBox.
I am submitting the small snippet here that might be useful for someone.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication6
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox new_textbox = new TextBox();
new_textbox.ID = "txt" + 1;
new_textbox.Text = "";
PlaceHolder1.Controls.Add(new_textbox);
}
protected void Button1_Click(object sender, EventArgs e)
{
string OptionID = "txt" + 1;
TextBox tb = (TextBox)PlaceHolder1.FindControl(OptionID);
Response.Write(tb.Text);
}
}
}
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Kumar SambhaveditedPosted May 17, 2013, 3:23 AMEdited May 17, 2013, 4:38 AM
Please check this codding is working properly in Windows Application ************************************************* using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace DynamicTexBox { public partial class Form2 : Form { public Form2() { InitializeComponent(); } TextBox[] textBoxes; private void button1_Click(object sender, EventArgs e) { int value = 3; textBoxes = new TextBox[value]; for (int i = 1; i < value; i++) { textBoxes[i] = new TextBox(); textBoxes[i].SetBounds(30, i * 30, 60, 20); this.Controls.Add(textBoxes[i]); } } private void button2_Click(object sender, EventArgs e) { int myTotal = 0; foreach (Control ctl in this.Controls) { if (ctl.GetType() == typeof(TextBox)) { myTotal += Convert.ToInt32(ctl.Text); } } label1.Text = myTotal.ToString(); } } }
Gihan PereraPosted Jan 3, 2011, 2:38 AM
i need get value from dynamic created textbox in windows forms(not web forms).