Solved

Trying to get FindControl to work

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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