FindControl not finding dynamic user control - Repeater, Panel, Usercontrol

Discussion in 'ASP .Net Web Controls' started by theComputer7, Jun 22, 2004.

  1. theComputer7

    theComputer7 Guest

    I cut down the code to make this half way understandable... I have
    read Data Grid girls caution about over use of dynamic controls. I
    truly believe what I am doing requires dynamically inserted user

    Worse I'm trying to add dynamic user controls from within a repeater
    loop (looping through attributes)... I bind to a function in the code
    behind and pass in the attribute.

    <asp:repeater id="rAttributes" Runat="server">
    <asp:panel id="attPanel" runat="server"></asp:panel>
    <%# InsertAttributeControl(Container)%>

    In the code behind I determine what type of attribute I'm dealing with
    (polymorphism) and create the appropriate control to render it. I
    create the control and insert it into a panel as google groups has

    // Load the UserControl.
    myLB theControl = (myLB)LoadControl("../Controls/myListBox.ascx");

    if (theControl != null)
    theControl.ID = "attControl";
    theControl.DataSource = theEnumAtt.Enums;
    theControl.DataTextField = "EnumName";
    theControl.DataValueField = "EnumID";

    // Add control to placeholder

    the user updates the form and submits to a wired event..... I spin
    through the repeater and get the panel (attPanel), but when I check
    the attPanel.HasControls() and it returns false. It seems the dynamic
    controls have disappeared.

    foreach (RepeaterItem ri in rAttributes.Items)
    // get controls
    Panel attPanel = ((Panel)ri.FindControl("attPanel"));
    myLB theAttEnum = ((myLB)attPanel.FindControl("attControl"));


    Any ideas?


    theComputer7, Jun 22, 2004
    1. Advertisements

  2. Did you recreate Controls on postback?
    Robert Koritnik, Jun 23, 2004
    1. Advertisements

  3. theComputer7

    Guest Guest

    So when the user submits... I try to retrieve the values. I have not
    even made it to a complete postback.. there is no reason to post back
    until I can at least capture the values the first time. Are you
    suggesting something?

    *** Sent via Devdex ***
    Don't just participate in USENET...get rewarded for it!
    Guest, Jun 23, 2004
  4. theComputer7

    Guest Guest

    I'm sorry.. I just figured out you have to recreate controls to retrieve
    values back. This aligns the controls with the post back values.

    But I'm still having trouble. I recreate the user control (list box)
    with all the items and then go to process the values. Now it finds the
    control, but the selected values are still gone.

    *** Sent via Devdex ***
    Don't just participate in USENET...get rewarded for it!
    Guest, Jun 23, 2004
  5. Yes that's because you recreated controls too late. Probably in the
    Page_Load event. That's why your postback data is gone. View state does not

    What can you do?
    Either recreate your controls in the OnInit event of the page (which I don't
    recommend, because people are overusing this and in the end if you don't
    know what you're doing it can get VERY dirty) or raise such an event that
    would deliver the values you need. Either create your own EventArguments
    implementation or use something that already can do something like that
    (Like Command event of the LinkButton class)
    Robert Koritnik, Jun 24, 2004
  6. theComputer7

    Guest Guest

    Actually it does work from the page load event... (The reason I wasn't
    getting values was a problem with my user control.)

    So now for everyone’s information.. my simple implementation for dynamic
    form elements (even inside a repeater) consists of letting the ASP.NET
    page build the repeater and then calling a custom method in the Page
    Load (LoadRepeaterWithDynamicControls). In the method I basically
    iterate through the repeater and place dynamic controls into each panel
    for each item.

    LoadRepeaterWithDynamicControls is called no matter if it's post back or
    first time. All attempts to grab the user entered values on post back
    must happen after the repeater is reloaded with the dynamic controls.

    The confusion I had was I thought the controls were holding the values
    on post back. But really on post back you have the values and just need
    to replace the controls to pick them up. Imagine that all values are
    being stored in a single hidden field and that the order of these values
    corresponds to the order of controls in the HTML document. So if you
    rebuild the controls the way they were... they will automatically pick
    back up their values... even if you data bind them again (ex. list box)
    and then try to retrieve the user selected values.

    The illusion of event based forms that .NET gives actually made me
    forget I'm just dealing with HTML and HTTP. :)

    *** Sent via Devdex ***
    Don't just participate in USENET...get rewarded for it!
    Guest, Jun 24, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.