I have an ascx control;
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LanguageSelect.ascx.cs" Inherits="MyNamespace.LanguageSelect" %>
with code behind:
protected void Page_Load(object sender, EventArgs e)
{
Languages = GetSiteLanguagesService();
if (Languages.Count > 1)
{
//null reference exception here!! languageDropdown is null!
LanguageDropdown.Visible = true;
LanguageDropdown.DataTextField = "DisplayName";
LanguageDropdown.DataValueField = "LangUrl";
LanguageDropdown.DataSource = Languages ;
LanguageDropdown.DataBind();
}
}
inside another control:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SomePage.ascx.cs" Inherits="myNamespace.SomePage" %>
<div id="main" role="main">
<myTag:LanguageSelect id="langSelect" runat="server" />
<div class="section columns">
<div class="main-column"> ....
With the debugger i hit on the page_load of LanguageSelect, but the dropdown is null! if i’m adding the drop down control in the markup, i thought it should always exists, in this case it is behaving like it was just a dynamic control..
now, the control SomePage is being rendered in the html, but this one in the myTag is not being rendered at all, does not matter if i wrap the dropdown list in a div, the div won’t be rendered either!!
If i put the breakpoint in the SomePage page_load, the langSelect control will be there (not null), but it itself has a null LanguageDropdown
Finally, figured out the problem; when the control is embedded in other control, ASP.NET assumes that you will override rendering (it will not render the sub-controls automatically)
I had to made the following changes to Page_Load, basically allocating the control:
and then, add an override for the OnRender handler:
After this, the control rendered perfectly!