0

Here is all my code:

Site.Master

<%@ Master Language="C#" AutoEventWireup="true"
CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>

<!DOCTYPE html>
<html>
    <head runat="server">
        <title></title>
        <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
        <asp:ContentPlaceHolder ID="HeadContent" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form runat="server">
            <div class="main">
                <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
            </div>
        </form>
    </body>
</html>

Site.Master.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
</asp:Content>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ListBox ListBox1 = new ListBox();
            ListBox1.FindControl("ListBox1");
            ListBox1.Items.Add(new ListItem("Hello"));
        }
    }
}

Question: Why is my ListBox not populating?

When running the code I get a page with a small box that is completely empty.

I tried the following already and I will tell you the result -

  • Remove

ListBox ListBox1 = new ListBox();
ListBox1.FindControl("ListBox1");

Result: The name 'ListBox1' does not exist in the current context

  • Add a reference to ListBox1 manually into the designer.

Result: Object reference not set to an instance of an object.

To fix that error adding the line ListBox ListBox1 = new ListBox(); works, but still does not display.

Any ideas? Thanks.

2 Answers 2

2

You should only need...

protected void Page_Load(object sender, EventArgs e)
        {
            ListBox1.Items.Add(new ListItem("Hello", "H"));
        }

See this link too

EDIT: Same code works for me.

enter image description here enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

I would be willing to bet that your _Default.aspx.cs does not contain a definition for ListBox1. If this entry is not present, manually add it
@JohnAtrik, I have added the 2nd arg (this isn't actually required). I have seen this before, and either clean solution -> rebuild, or restart VS.
@Jeff Fritz I did do that as I said and got the error mentioned above.
@christiandev The addition of the "H" did nothing, still says that Object reference not set to an instance of an object.
Ack, I listed the wrong filename... You should have a definition in Default.aspx.designer.cs That is where all controls in the markup are mapped to c-sharp.
|
0

To directly answer your question of why ListBox1 isn't getting populated:

protected void Page_Load(object sender, EventArgs e)
{
     ListBox ListBox1 = new ListBox();  //You are creating a new local ListBox named ListBox1.
     ListBox1.FindControl("ListBox1");  //You are trying to find the Control named ListBox1 that is held within the Control ListBox1 (and not doing anything with the result of the search)
     ListBox1.Items.Add(new ListItem("Hello"));  //You are adding a new ListItem to the local ListBox1.
}

As christiandev said, you should just be able to do this:

protected void Page_Load(object sender, EventArgs e)
{
     ListBox1.Items.Add(new ListItem("Hello"));
}

I have no idea why you can't seem to access the ListBox1 on the aspx page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.