Hi
I am having an issue when using LoadControl( type, Params )
. Let me explain...
I have a super simple user control (ascx)
<%@ Control Language="C#" AutoEventWireup="True" Inherits="ErrorDisplay" Codebehind="ErrorDisplay.ascx.cs" EnableViewState="false" %>
<asp:Label runat="server" ID="lblTitle" />
<asp:Label runat="server" ID="lblDescription" />
with code ( c# ) behind of:
public partial class ErrorDisplay : System.Web.UI.UserControl
{
private Message _ErrorMessage;
public ErrorDisplay()
{
}
public ErrorDisplay(Message ErrorMessage)
{
_ErrorMessage = ErrorMessage;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (_ErrorMessage != null)
{
lblTitle.Text = _ErrorMessage.Message;
lblDescription.Text = _ErrorMessage.Description;
}
}
}
Elsewhere in my web application I am adding an instance of the usercontrol to the page using the following code:
divValidationIssues.Controls.Add(LoadControl(typeof(ErrorDisplay), new object[] { MessageDetails }));
I am using the overloaded version of LoadControl because I want to pass the Message parameter to the constructor. All this appears to work ok.
However, when the OnPreRender()
is fired on the ErrorDisplay usercontrol the lblTitle and lblDescription variables are both null
, despite them having a markup equivalent. The message variable has been correctly populated.
Can anyone shed any light on why this may be happening?
Thanks
EDIT:
Just for clarity I'll also add that the code which is programatically adding the usercontrol to the page is running in response to a button press, so the 'hosting page' has progressed through Init, Page_Load and is now processing the event handlers.
I cannot add the usercontrols at an earlier asp lifecycle stage as they are being created in response to a button click event.