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

T

theComputer7

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
 
G

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 http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
G

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 http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
R

Robert Koritnik

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)
 
G

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 http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top