Creating Dynamic TextBox and Get Its Value


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);
    
          
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all