page or parent properties are null in server control

T

TS

When i try to access the page class or parent properties inside a server
control that is nested in another server control, they are null. I don't
even call CreateChildControls for the parent control until its pre-render
event, so i know the parent control is on the page.

Any ideas?

thanks
 
T

TS

i can get handler and cast as page and that works though.

(Page)HttpContext.Current.Handler
 
T

TS

What happens is the control is added to the parent control's e.item property
during it's itemDataBind event. The aspx page calls this parent control's
databind method during page_Load. The parent control's code is as follows:

protected override void OnItemDataBound(DataListItemEventArgs e)

{

base.OnItemDataBound(e);

Parameter parameter = (Parameter)e.Item.DataItem;

try

{

Control control;

if(parameter.Type == ParameterType.Control)

control = this.Page.LoadControl(parameter.ControlPath);

else

{

Assembly assembly = this.Page.GetType().BaseType.Assembly;

//Assembly assembly =
Assembly.LoadFrom(@"C:\Tea\ACES\_TEAMS\Web\bin\TEA.Teams.Web.dll");

control = (Control) assembly.CreateInstance(parameter.ControlTypeName);

}

control.ID = parameter.Name;

// Set up all the properties of this control from the parameter.properties

foreach(DictionaryEntry de in parameter.Properties)

SetProperty(de.Key.ToString(), de.Value.ToString(), control);


e.Item.Controls.Add(control);

}

catch (Exception ex)

{

throw new ApplicationException("Unable to render criteria. Reason: " +
ex.Message, ex);

}

}
 
T

TS

sorry to continue this again:

The child control's CreateChildControls is fired as soon as the
e.item.controls.Add method adds it

thanks
 
S

Steven Cheng[MSFT]

Hi TS,

For ASP.NET server controls, the "Parent" property and "NamingContainer",
they'll be set when being added into a certain ControlCollection. Also
,from your description, the control you're developing seems to be a
databound control, and has the ItemDataBound event. If you added the
control in ItemDataBound event, then when do you check the control's parent
and found the value be null? It'll be much more helpful if you could
provide some further description on the custom control you're developing.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)






--------------------
| From: "TS" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: page or parent properties are null in server control
| Date: Thu, 28 Jul 2005 17:57:27 -0500
| Lines: 97
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 103nat100.tea.state.tx.us 198.214.103.100
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:115063
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| sorry to continue this again:
|
| The child control's CreateChildControls is fired as soon as the
| e.item.controls.Add method adds it
|
| thanks
|
| | > What happens is the control is added to the parent control's e.item
| property
| > during it's itemDataBind event. The aspx page calls this parent
control's
| > databind method during page_Load. The parent control's code is as
follows:
| >
| > protected override void OnItemDataBound(DataListItemEventArgs e)
| >
| > {
| >
| > base.OnItemDataBound(e);
| >
| > Parameter parameter = (Parameter)e.Item.DataItem;
| >
| > try
| >
| > {
| >
| > Control control;
| >
| > if(parameter.Type == ParameterType.Control)
| >
| > control = this.Page.LoadControl(parameter.ControlPath);
| >
| > else
| >
| > {
| >
| > Assembly assembly = this.Page.GetType().BaseType.Assembly;
| >
| > //Assembly assembly =
| > Assembly.LoadFrom(@"C:\Tea\ACES\_TEAMS\Web\bin\TEA.Teams.Web.dll");
| >
| > control = (Control) assembly.CreateInstance(parameter.ControlTypeName);
| >
| > }
| >
| > control.ID = parameter.Name;
| >
| > // Set up all the properties of this control from the
parameter.properties
| >
| > foreach(DictionaryEntry de in parameter.Properties)
| >
| > SetProperty(de.Key.ToString(), de.Value.ToString(), control);
| >
| >
| > e.Item.Controls.Add(control);
| >
| > }
| >
| > catch (Exception ex)
| >
| > {
| >
| > throw new ApplicationException("Unable to render criteria. Reason: " +
| > ex.Message, ex);
| >
| > }
| >
| > }
| >
| > | > > i can get handler and cast as page and that works though.
| > >
| > > (Page)HttpContext.Current.Handler
| > >
| > >
| > > | > > > When i try to access the page class or parent properties inside a
| server
| > > > control that is nested in another server control, they are null. I
| don't
| > > > even call CreateChildControls for the parent control until its
| > pre-render
| > > > event, so i know the parent control is on the page.
| > > >
| > > > Any ideas?
| > > >
| > > > thanks
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|
 

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

Top