Why OnPreInit unvailable on controls

  • Thread starter Thread starter WT
  • Start date Start date
W

WT

Hello,

I can't see OnPreInit method for Webcontrols and controls.
Why the method is not similarly called for controls as it is done for Page
Control ?
It is sometime uneasy to initialize SkinID for dynamic controls.
In the past we had the OnInit method of the custom control to initialize
everything, but we can't initialize SkinID in OnPreinit, getting an
exception. And as far as no OnPreInit exists, how to initialize SkinID for
dynamic controls???

Any help welcome, waiting for a correction from MS ?

Thanks
CS
 
Hi CS,

Yes unfortunately PreInit is only available in Page class, it's not
available in Control class.

However, based on my understanding, a control normally is designed to be
used by Page. A control should not hard-code a skin, instead it should let
the Page choose one in Page's PreInit:

#How to: Apply ASP.NET Themes Programmatically
http://msdn2.microsoft.com/en-us/library/tx35bd89(vs.80).aspx

You also mentioned "initialize SkinID for dynamic controls", do you mean
that the control is created dynamically? If this is the case, it should be
able to set its SkinID when it's created in Page's PreInit.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Lets say that my constrols are custom controls loaded dynamically fromdb
with loadcontrol.
So where to place the skinID init ?
I have tried to use the PreInit event but unsucessfully ?

CS
 
Hi CS,

You could create a public property to wrap the consitituent control's
SkinID and assign this property in Page's PreInit:

WebUserControl.ascx.cs:

public string TextBoxSkinID
{
get { return txt1.SkinID; }
set { txt1.SkinID = value; }
}

Page.aspx.cs:

protected override void OnPreInit(EventArgs e)
{
WebUserControl wuc =
(WebUserControl)LoadControl("~/WebUserControl.ascx");
wuc.TextBoxSkinID = "BlueTheme";
form1.Controls.Add(wuc);

base.OnPreInit(e);
}


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
In fact I need to lad the custom controls in Page.OnPreInit ?
They were originally loaded in Page.OnDataBinding.
Problem is OnDatabinding is it called from OpreInit or after ?
CS
 
Hi CS,

It seems I may have misunderstood something regarding the PreInit event.
It's actually only Page.Theme property can be set in PreInit. Setting
Control.SkinID should be fine in Page_Load or during DataBinding:

<%@ Page Language="C#" Theme="Theme1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
TextBox txt1 = new TextBox();
txt1.Text = "Page_Load";
txt1.SkinID = "BlueTheme";
form1.Controls.Add(txt1);

this.DataBinding += new EventHandler(default2_aspx_DataBinding);
this.DataBind();
}

void default2_aspx_DataBinding(object sender, EventArgs e)
{
TextBox txt1 = new TextBox();
txt1.Text = "DataBinding";
txt1.SkinID = "BlueTheme";
form1.Controls.Add(txt1);
}

</script>


I'm sorry if I may have caused some confusion in my previous replies.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Walter,

So why I get this exception trying to initialize a SkinID:
(this has been solved by initializing SkinID in PreInit for dynamic
controls)

ProcessUnhandledException enter GetLastError:
System.Web.HttpUnhandledException: Exception of type
'System.Web.HttpUnhandledException' was thrown. --->
System.InvalidOperationException: The 'SkinId' property can only be set in
or before the Page_PreInit event for static controls. For dynamic controls,
set the property before adding it to the Controls collection.
at System.Web.UI.Control.set_SkinID(String value)
at System.Web.UI.WebControls.WebControl.set_SkinID(String value)
at AddonNice.UI.WebControls.PortalModuleControl.OnInit(EventArgs e)
at Ax.WTPageModules.PortalsAdmin.Portals.OnInit(EventArgs e)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.AddedControl(Control control, Int32 index)
at System.Web.UI.ControlCollection.Add(Control child)
at System.Web.UI.WebControls.Table.RowControlCollection.Add(Control
child)
at Ax.UI.WebControls.DesktopPanes0.CreateControlHierarchy()
at Ax.UI.WebControls.DesktopPanes0.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
--- End of inner exception stack trace ---
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.desktopdefault_aspx.ProcessRequest(HttpContext context)
at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)
 
Hi CS,

If you're set a constituent control's SkinID in UserControl's OnInit or
Page_Load event, these events are still fired after you added the
UserControl to page's control tree, therefore you will see the exception.

There're two workarounds:

1) Use a public property to expose the constituent control's SkinID, and
set this property before adding the UserControl to control tree:

WebUserControl wc = (WebUserControl)LoadControl("~/WebUserControl.ascx");
wc.TextBoxSkinID = "BlueTheme";
form1.Controls.Add(wc);

2) If you want to hard-code SkinID for your constituent controls, you could
create a public method to do that and let the Page call this method before
adding the UserControl to control tree:

WebUserControl wc = (WebUserControl)LoadControl("~/WebUserControl.ascx");
wc.SetSkinID();
form1.Controls.Add(wc);

Hope this helps.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top