Solved

Trying to get FindControl to work

Posted on 2013-01-03
1
465 Views
Last Modified: 2013-01-15
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>";
                        divFieldsSpace.Controls.Add(myLabel);

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


                        divFieldsSpace.Controls.Add(myTextBox);

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

                    }

                }


            }
        }

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.
-Jason

//code always hits the catch, txtVarchar1 within divFieldSpace is never found...
         string varchar1 
           
             try
            {
             
                varchar1 = ((TextBox)divFieldsSpace.FindControl("txtVarchar1")).Text;
            }
            catch
            {
                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

0
Comment
Question by:jazzcatone
1 Comment
 
LVL 11

Accepted Solution

by:
b_levitt earned 500 total points
ID: 38741262
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:
http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question