简体   繁体   中英

asp:dropdownlist is null on page_load

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

  1. Is your UserControl added dynamically to the page? I'm asking because people sometimes to forget that they must create an instance of it via Page.LoadControl instead of using the constructor.
  2. Have you registered the UserControl in the page?

For example:

<%@ Register TagPrefix="uc1" TagName="Lang" Src="controls/LanguageSelect.ascx" %>

Side-note: It's almost always a bad idea to use page events to inititialize the UserControl implicitely. Instead the controller(normally the page) should initialize the control directly(via calling a method like BindData or Init ). Otherwise it can lead to lifecycle issues or nasty errors.

Try to delete the auto-generated ".ascx.designer.cs" file and regenerate it. To do this, follow the steps:

  • delete the .ascx.*.designer.cs
  • right-click the ascx file and select "Convert to Web Application"

Now the designer.cs file is recreated. See if the things are gonna work now.

It might be trying to load your UserControl (.ascx) as a CustomControl (no .ascx). Check your page and web.config pages/controls section for references to LanguageSelect 's namespace instead of the .ascx path.

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:

protected void Page_Load(object sender, EventArgs e)
        {
            Languages = GetSiteLanguagesService();
            if (Languages.Count > 1)
            {

                LanguageDropdown = new DropDownList(); //allocate the control
                LanguageDropdown.Visible = true;
                LanguageDropdown.DataTextField = "DisplayName";
                LanguageDropdown.DataValueField = "LangUrl";
                LanguageDropdown.DataSource = Languages ;
                LanguageDropdown.DataBind();
            }
        }

and then, add an override for the OnRender handler:

protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            LanguageDropdown.RenderControl(writer);
        }

After this, the control rendered perfectly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM