• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Trying to get FindControl to work

I have some labels and textboxes that I need to create within a parentcontrol called"divFieldsSpace". This happens on the SelectedIndex change of a dropdown. This works fine. (See Code)
        protected void ddlCV_category_OnSelectedIndexChanged(object sender, EventArgs e)
            ClaimInfo ci = new ClaimInfo();
            DataSet ds;
            ds = ci.GetvendorCategoryFormFields(Convert.ToInt32(ddlCV_category.SelectedValue));

            foreach (DataRow dr in ds.Tables[0].Rows)
                foreach (DataColumn column in dr.Table.Columns)
                    if (dr[column].ToString().Trim() != "")
                        Label myLabel = new Label();
                        myLabel.Text = "<b>" + dr[column].ToString() + "</b>";

                        TextBox myTextBox = new TextBox();
                        myTextBox.ID = "txt" + column.ColumnName;


                        Label myBreakLabel = new Label();
                        myBreakLabel.Text = "<br><br>";




Open in new window

However, I then need to get the value entered in the textboxes after a button click. No matter what I do FindControl() never seems to work in trying to get the textboxes. As mentioned before , I'm almost certain I know the parent control (divFieldsSpace) so I'm somewhat at a loss at this point. The textboxes show up right where they are suppose to. Is there something else I need to do upon creation of the textboxes. I do have multiple pageviews on the page this runs on , but again, I would think as long as I knew the parent control I would be in good shape. Any advice or direction would be greatly appreciated.

//code always hits the catch, txtVarchar1 within divFieldSpace is never found...
         string varchar1 
                varchar1 = ((TextBox)divFieldsSpace.FindControl("txtVarchar1")).Text;
                varchar1 = "";

//I even tried a recursive search to see if I could see child controls... there's nothing
    foreach (Control childControl in divFieldsSpace.Controls)
                string name = childControl.ID;


Open in new window

1 Solution
Controls are created during initialization and then merged with viewstate and postback data during load.  However, your controls are being created in code and you're only creating them the one time.

Dynamically created controls need to be recreated on EVERY postback with the same ID.  You'll find that if you rerun your code from your selectedindexchanged event prior to handling the button click that they will have been merged with the posted viewstate and postback info.

See the asp.net page lifecycle for more info:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now