0

Google seems to think I should be able to do this: Basically I'm creating textboxes/checkboxes in a listview in code behind and I'm try to get their values. Easy enough, each item has a panel and in that is a hiddenfield with the id value and a textbox or checkbox.

On post back I'm recreating the controls and then searching for the values, by iterating through each item and checking for values. But I can't find the controls within the panel.

Here is my watch panel when it claims it can't find my control by ID: enter image description here

Here's the code to find the control:

foreach(var item in lvAssetProperties.Items)
    {hfPropID = item.FindControl("hfPropID") as HiddenField;
        int id;
        if (int.TryParse(hfPropID.Value, out id))
        {
           prop = ctx.Properties.FirstOrDefault(p => p.id == id);
           if (prop.Type_Property.Select(t => t.Type.id).Contains(type.id))
           {
               var cnt = item.FindControl(string.Format("Prop_{0}", prop.id));
               if (cnt is TextBox)
               {
                  var value = (cnt as TextBox).Text;
               }
           }
       }
   }
Tod
  • 2,070
  • 21
  • 27
  • Where exactly are you recreating the controls? – Blachshma Dec 18 '14 at 12:18
  • The function to bind the listview is called by Page_Init, the listview has a on itemcreated function that adds the controls. You can see from the watch "item.FindControl("pnlInput").Controls[1].ID" that is has at least two controls, and I can tell you those controls are only added dynamically. – Tod Dec 18 '14 at 13:10
  • Have you tried to break it up where you find the `hfPropID = item.FindControl("Panel1") as Panel;`. Then try to find the textbox in that panel instance `txtBox = hfPropID.FindControl("txtBox1") as TextBox`? – user3841709 Dec 18 '14 at 15:12
  • Sorry yes, thats what I was doing in the watch, I appreciate the code example won't work as findcontrol isn't reciprocal. – Tod Dec 18 '14 at 15:20

0 Answers0