[英]WPF Listbox content showing Radio buttons list. Check default radio button from the list initially
[英]List content not showing in 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();
}
}
}
}
但是,列表框中沒有文本。 我認為存在錯誤,因為列表中的數據找不到通往ListBox的方式。 但是在Visual Studio中未顯示任何錯誤。 我檢查了幾次代碼,但仍然找不到問題所在。 有人可以幫我找出造成問題的原因嗎?
您正在調用dbb.getArr()
但實際上並沒有執行任何操作。 您需要將檢索到的值綁定到ListBox,而不僅僅是將它們寫入屏幕。
遵循這些原則的東西至少應該為您指明正確的方向。 首先,從getArr()
返回string[]
。
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.