Solved

Trying to get FindControl to work

Posted on 2013-01-03
1
462 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now