列出未在ListBox中显示的内容

时间:2016-01-05 18:31:13

标签: c# asp.net listbox

我正在ASP.NET中构建一个用于拍卖的应用程序,其中列表从数据库中提取数据并在ListBox中显示。我有Auction.cs

namespace WebApplication5
{
    public class Auction
    {
        public string Productname { get; set; }
        public string Lastbidder { get; set; }
        public int Bidvalue { get; set; }

        public override string ToString()
        {
            return "ProductName: " + Productname + "\nLastbidder: " + Lastbidder + "\nBidvalue: " + Bidvalue;
        }
    }

    public class AuctionService
    {
        private List<Auction> listaAukcija = new List<Auction>();

        public List<Auction> ListaAukcija
        {
            get { return listaAukcija; }
            set { listaAukcija = value; }
        }

        public void getAll()
        {
            using (SqlConnection conn = new SqlConnection(@"data source=JOVAN-PC;database=aukcija_jovan_gajic;integrated security=true;"))
            {
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT a.id AS aid, p.name AS pn, u.name AS un, a.lastbid AS alb FROM auctions a JOIN products p ON a.productid = p.id JOIN users u ON a.lastbider = u.id";

                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    listaAukcija.Clear();

                    while (reader.Read())
                    {
                        Auction auction = new Auction();

                        if (reader["aid"] as int? != null)
                        {
                            auction.Productname = reader["pn"] as string;
                            auction.Lastbidder = reader["un"] as string;
                            auction.Bidvalue = (int)reader["alb"];
                        }

                        listaAukcija.Add(auction);
                    }
                }
            }
        }
    }
}

DbBroker.cs

namespace WebApplication5
{
    public class DbBroker
    {
        AuctionService aukcija = new AuctionService();

        public void executeQuery()
        {
            aukcija.getAll();
        }

        public void getArr()
        {
            string[] lista = aukcija.ListaAukcija.ConvertAll(obj => obj.ToString()).ToArray();

            Console.WriteLine (string.Join("\n", lista));
        }  
    }
}

以及一个名为Home.aspx的页面,后面带有代码:

namespace WebApplication5
{
    public partial class Home : System.Web.UI.Page
    {
        DbBroker dbb = new DbBroker();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label3.Text = Session["Username"].ToString();
                dbb.executeQuery();
                dbb.getArr();
            }

        }
    }
}

问题是Home.aspx应该是这样的: enter image description here

但ListBox中没有文字。我假设有一个错误,因为列表中的数据没有找到ListBox的方式。但Visual Studio中没有显示错误。我检查了几次代码,但我仍然无法找到问题所在。有人可以帮我找到造成问题的原因吗?

1 个答案:

答案 0 :(得分:1)

您正在致电dbb.getArr(),但您实际上并没有采取任何措施。您需要将检索到的值绑定到ListBox,而不是将它们写入屏幕。

沿着这些方向的东西至少应该指向正确的方向。首先,从string[]返回getArr()

public string[] getArr()
{
    string[] lista = aukcija.ListaAukcija.ConvertAll(obj => obj.ToString()).ToArray();
    return lista;
}

然后将该字符串数组设置为ListBox的DataSource并绑定它。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Label3.Text = Session["Username"].ToString();

        dbb.executeQuery();
        string[] lista = dbb.getArr();

        listbox.DataSource = lista;
        listbox.DataBind();
    }
}