ASP.NET ListBox麻烦

时间:2013-08-14 13:40:01

标签: c# asp.net listbox

这是我的所有代码:

的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"));
        }
    }
}

问题:为什么我的ListBox没有填充?

运行代码时,我得到一个页面,其中包含一个完全空的小框。

我已经尝试了以下内容,我会告诉你结果 -

  • 删除

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

结果:当前上下文中不存在名称“ListBox1”

  • 手动将ListBox1的引用添加到设计器中。

结果:对象引用未设置为对象的实例。

要解决该错误,请添加第ListBox ListBox1 = new ListBox();行,但仍无法显示。

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:2)

你应该只需要......

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

另见link

编辑:相同的代码适用于我。

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

答案 1 :(得分:0)

直接回答您为什么ListBox1没有填充的问题:

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.
}

正如christiandev所说,你应该能够做到这一点:

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

我不知道为什么你似乎无法访问aspx页面上的ListBox1。