Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:57:06+00:00 2026-06-15T07:57:06+00:00

I am creating some dyanamic textbox on a button click event and on another

  • 0

I am creating some dyanamic textbox on a button click event and on another button click i want to fetch data of that textbox using findcontrol method

 public void addDepartmentBtn_Click(object sender, EventArgs e)
    {

        int count = Convert.ToInt32(countTxtBx.Text);
        lblErrorMsg.Text = "";
        if (Convert.ToInt32(countTxtBx.Text) <= 5)
        {

            for (int i = 0; i < count; i++)
            {
                Label lb = new Label();
                TextBox tb = new TextBox();

                tb.ID = "Textbox_" + i;
                lb.ID = "Label_" + i;
                lb.Text = "Enter Departnment Name: " + Convert.ToInt32(i + 1);
                pnlMain.Controls.Add(new LiteralControl("<br>"));
                pnlMain.Controls.Add(lb);
                pnlMain.Controls.Add(new LiteralControl("&nbsp&nbsp"));
                pnlMain.Controls.Add(tb);
                pnlMain.Controls.Add(new LiteralControl("<br><br>"));
                lblErrorMsg.Text = Convert.ToInt32(i + 1) + "  Departments Created Successfully";
                //string str = string.Empty;
                //TextBox myTB = (TextBox)pnlMain.FindControl("Textbox_" + i);
                //str = myTB.Text;
                //Response.Write(str);

            }

        }
        else
        {
            lblErrorMsg.Text = "You cannot create more than 5 Departments at once:";
        }
    }

On button2 click:

   protected void Button2_Click(object sender, EventArgs e)
        {
            string alltextdata = null;
            for (int i = 0; i < 5; i++)
            {
                Control controltxt = FindControl("Textbox_"+i);
                if (controltxt != null)
                {
                    TextBox txttemp = (TextBox)controltxt;
                    alltextdata = txttemp.Text;

                }
            }

        }

but my find control method alway show me null i check my html page view source which show me every thing correct my textbox name and id is “Textbox_0”,Textbox_1 etc

am i doing some mistake ? please help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:57:07+00:00Added an answer on June 15, 2026 at 7:57 am

    When you add the control dynamically, it is not added to the control tree after a postback (the button2 postback). You need to add it again in the Page_Load event in any postback after addDepartmentBtn was clicked.

    Save that button was clicked in the ViewState and check it in Page_Load:

    public void addDepartmentBtn_Click(object sender, EventArgs e)
        {
            ViewState["addDepartmentBtn_Clicked"] = true;
            AddTextBoxes();
        }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToBoolean(ViewState["addDepartmentBtn_Clicked"]) == true)
            AddTextBoxes();
    }
    
    public void AddTextBoxes()
    {
    int count = Convert.ToInt32(countTxtBx.Text);
            lblErrorMsg.Text = "";
            if (Convert.ToInt32(countTxtBx.Text) <= 5)
            {
    
                for (int i = 0; i < count; i++)
                {
                    Label lb = new Label();
                    TextBox tb = new TextBox();
    
                    tb.ID = "Textbox_" + i;
                    lb.ID = "Label_" + i;
                    lb.Text = "Enter Departnment Name: " + Convert.ToInt32(i + 1);
                    pnlMain.Controls.Add(new LiteralControl("<br>"));
                    pnlMain.Controls.Add(lb);
                    pnlMain.Controls.Add(new LiteralControl("&nbsp&nbsp"));
                    pnlMain.Controls.Add(tb);
                    pnlMain.Controls.Add(new LiteralControl("<br><br>"));
                    lblErrorMsg.Text = Convert.ToInt32(i + 1) + "  Departments Created Successfully";
                    //string str = string.Empty;
                    //TextBox myTB = (TextBox)pnlMain.FindControl("Textbox_" + i);
                    //str = myTB.Text;
                    //Response.Write(str);
    
                }
    
            }
            else
            {
                lblErrorMsg.Text = "You cannot create more than 5 Departments at once:";
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating some basic OO scripts using MySQLi and getting an Undefined Method error
I am creating some PDF reports using iText in Java. According to the requirements,
I'm creating some entities (class) for my project and I want to set a
I am creating an application that tracks some specific performance-related statistics from SQL Server
I'm having problems with some dynamic elements that I'm creating with Jquery. for(var i
I'm successfully creating some dynamic input textboxes using the following javascript: var type =
I am considering creating some JSP-tags that will always give the same output. For
I'm creating some client side functions for a mappable spreadsheet export feature. I'm using
I am currently creating a site with some dynamic content, so that whenever a
I'm currently using Generics to make some dynamic methods, like creating an object and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.