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
    controls.

    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">
    <ItemTemplate>
    <asp:panel id="attPanel" runat="server"></asp:panel>
    <%# InsertAttributeControl(Container)%>
    </ItemTemplate>
    </asp:repeater>

    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
    suggested...


    // 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";
    theControl.DataBind();

    // Add control to placeholder
    attPanel.Controls.Add(theControl);
    }





    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?

    Thanks,

    Don
    theComputer7, Jun 22, 2004
    #1
    1. Advertising

  2. Did you recreate Controls on postback?

    --
    RobertK
    { Clever? No just smart. }

    "theComputer7" <> wrote in message
    news:...
    > 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
    > controls.
    >
    > 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">
    > <ItemTemplate>
    > <asp:panel id="attPanel" runat="server"></asp:panel>
    > <%# InsertAttributeControl(Container)%>
    > </ItemTemplate>
    > </asp:repeater>
    >
    > 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
    > suggested...
    >
    >
    > // 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";
    > theControl.DataBind();
    >
    > // Add control to placeholder
    > attPanel.Controls.Add(theControl);
    > }
    >
    >
    >
    >
    >
    > 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?
    >
    > Thanks,
    >
    > Don
    Robert Koritnik, Jun 23, 2004
    #2
    1. Advertising

  3. 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 http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
    don't spam me pleast no spam, Jun 23, 2004
    #3
  4. 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 http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
    theComputer7 - no spam, Jun 23, 2004
    #4
  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
    persist.

    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)

    --
    RobertK
    { Clever? No just smart. }


    "theComputer7 - no spam" <> wrote in message
    news:#...
    > 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 http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Robert Koritnik, Jun 24, 2004
    #5
  6. 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 http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
    theComputer7 - no spam, Jun 24, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nail
    Replies:
    0
    Views:
    1,535
  2. Ulf Meinhardt
    Replies:
    0
    Views:
    606
    Ulf Meinhardt
    Jul 7, 2009
  3. Robin Wenger
    Replies:
    0
    Views:
    2,920
    Robin Wenger
    Aug 18, 2011
  4. Peter Kaufman

    error on findcontrol(MyPanel,panel)

    Peter Kaufman, Jan 24, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    97
    Peter Kaufman
    Jan 24, 2004
  5. Ian Hobson
    Replies:
    1
    Views:
    323
    Hidetoshi NAGAI
    Dec 7, 2003
Loading...

Share This Page