Problem with WebControl and dynamic UserControl..

R

Rocky Moore

I am trying to make a WebControl as a content panel which given the user
control name and location, will create the specified user control to fill
the content of the WebControl. It can be any user control which may contain
buttons, edits, etc. and require postbacks.

At this point, I dynamically create and add the control to the
WebControl.Controls in the CreateChildren. Then in the Render, I specify to
RenderChildren. It all appears ok but the problem is I do not see how to
hookup validitors or state in the UserControl that is created.

Any ideas, if this task is even possible?
 
R

Rocky Moore

Not quite the target. I would like to have a ContentPanel that can display
_any_ UserControl and allow the UserControl to function as normal. The
ContentPanel would only know the filename and folder of the control and
nothing more about it.

I can currently obtain the functionality I desire by putting the code to
dynamically generate an instance of the UserControl in the Page CodeBehind
but if I put that code in a WebControl, the UserControl no longer maintains
state and the Validators on differnet controls in the UserControl are never
triggered in the UserControl.

At this point, I am sure I am missing something that the WebControl is
limiting on the UserControl, but not sure what it is. The UserControl is
not getting any postback. I can set break points or traces all over any of
the UserControls and only the constructors are being called. None of the
custom validators. Of couse, the fact that no data is persisting was
another clue to me ;)

Thanks!
 
J

John Saunders

Rocky,

I've just had a great deal of luck with figuring out what was going on in
the control lifecycle by overriding almost every method that can be
overridden, tracing, and then calling the base class version. Here's a list:

LoadViewState, SaveViewState, TrackViewState
OnInit, OnLoad, OnPreRender, OnUnload
OnBubbleEvent
DataBind, OnDataBinding
Render

In each case, I had one line of Page.Trace.Write, followed by calling the
base class version as appropriate.

Note a problem with Page.Trace. It fails badly at design time. I centralized
my "Page.Trace.Write" calls and added the following code:


Debug.WriteLine(string.Format("{0:hh:mm:ss.ff} {1}", DateTime.Now, message),
category);
if (!InDesignMode(control) && CanTrace(control))
{
control.Page.Trace.Write(category, message, errorInfo);
}

....

private static bool InDesignMode(Control control)
{
return control != null && control.Site != null &&
control.Site.DesignMode;
}

// Note that sometimes, Page.Trace is non-null, yet throws an exception when
referenced
private static bool CanTrace(Control control)
{
if (control.Page != null)
{
try
{
return (control.Page.Trace != null);
}
catch
{
return false;
}
}
else
{
return false;
}
}

This almost returned too much information. When I decided I wasn't getting
enough information overload, I formatted and dumped out the contents of each
control's ViewState on LoadViewState and SaveViewState, and now my page
traces are about as large as the Sunday paper...

John Saunders
(e-mail address removed)
 
R

Rocky Moore

Thanks for the help. I discovered my problem that I should have noticed in
the first place. It needed the INamingContainer interface to allow the
UserControl to function properly. I figured it had to be something simple.
Just could not see the forest through all the trees ;)

Rocky
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top