1

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

2 Answers 2

2

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:";
        }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Before calling your findcontrol method, you have to load that contro again as it is dynamically created on each and every postback. These are not like our static controls which are created under Page_Init. Dynamic controls are created under Page_Load event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.