Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to get FindControl to work

Posted on 2013-01-03
1
Medium Priority
?
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
b_levitt earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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