Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > FindControl not finding dynamic user control - Repeater, Panel, Usercontrol

Reply
Thread Tools

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

 
 
theComputer7
Guest
Posts: n/a
 
      06-22-2004
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>
<aspanel id="attPanel" runat="server"></aspanel>
<%# 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
 
Reply With Quote
 
 
 
 
Robert Koritnik
Guest
Posts: n/a
 
      06-23-2004
Did you recreate Controls on postback?

--
RobertK
{ Clever? No just smart. }

"theComputer7" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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>
> <aspanel id="attPanel" runat="server"></aspanel>
> <%# 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



 
Reply With Quote
 
 
 
 
don't spam me pleast no spam
Guest
Posts: n/a
 
      06-23-2004
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!
 
Reply With Quote
 
theComputer7 - no spam
Guest
Posts: n/a
 
      06-23-2004
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!
 
Reply With Quote
 
Robert Koritnik
Guest
Posts: n/a
 
      06-24-2004
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" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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!



 
Reply With Quote
 
theComputer7 - no spam
Guest
Posts: n/a
 
      06-24-2004


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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FindControl fails in event handler for usercontrol Bill E. ASP .Net 5 01-08-2010 02:24 PM
FindControl() on ASP.NET UserControl Objects =?Utf-8?B?QWxleCBNYWdoZW4=?= ASP .Net 14 05-24-2007 08:42 AM
FindControl() on a UserControl Alex Maghen ASP .Net Building Controls 1 05-14-2007 05:24 AM
Getting a property of a control inside a usercontrol, from another usercontrol Fabiano ASP .Net 2 06-03-2005 09:56 AM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM



Advertisments