我一直在使用DynamoDb进行ASP.NET项目,我已经从Db中提取了该项目并将其制作为新的Car对象。当我尝试填充汽车的ListBox时,似乎在ListBox中没有任何对象。这是我所拥有的:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Amazon.DynamoDBv2;using Amazon.DynamoDBv2.Model;using Amazon.DynamoDBv2.DocumentModel;public partial class _Default : System.Web.UI.Page{ private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); private static string tableName = "CarTable"; private static string sampleCarReg = "05-Rn-1964"; List<car> cars = new List<car>(); protected void Page_Load(object sender, EventArgs e) { Amazon.DynamoDBv2.DocumentModel.Table CarTable = Amazon.DynamoDBv2.DocumentModel.Table.LoadTable(client, tableName); GetItemOperationConfig config = new GetItemOperationConfig { AttributesToGet = new List<string> { "Reg", "Manufactor", "Year", "Problems" }, ConsistentRead = true }; Document doc = CarTable.GetItem(sampleCarReg, config); string Reg = doc["Reg"].AsString(); string Manufactor = doc["Manufactor"].AsString(); string Year = doc["Year"].AsString(); string Problems = doc["Problems"].AsString(); // string Model = doc["Model"].AsString(); car car1 = new car(Reg, Manufactor, Year, Problems); cars.Add(car1); lblCar.Text = car1.ToString(); lbxCars.DataSource = cars; }
1 回答
jeck猫
TA贡献1909条经验 获得超7个赞
您正在设置ListBox的DataSource属性,但未指定DataTextField和DataValueField。您需要设置Car中使用的属性ListBox。
// Use what makes sense to you
lbxCars.DataSource = cars;
lbxCars.DataTextField = "Reg";
lbxCars.DataValueField = "Reg";
lbxCars.DataBind();
如果您还想选择第一项,请使用该SelectedIndex属性。当然,请务必先检查您的汽车清单中是否有物品。
if (cars.Any())
{
lbxCars.SelectedIndex = 0;
}
- 1 回答
- 0 关注
- 131 浏览
添加回答
举报
0/150
提交
取消