2 回答

TA貢獻1851條經驗 獲得超5個贊
這是關于 web form 的問題,當你想在 bulletedlist 中顯示自定義類的屬性時,你應該將 bulletedlist 的 DataTextField 設置為該屬性的名稱。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Cottages> list = new List<Cottages>();
list.Add(new Cottages { MyText = "text1", MyValue = "value1" });
list.Add(new Cottages { MyText = "text2", MyValue = "value2" });
list.Add(new Cottages { MyText = "text3", MyValue = "value3" });
***BulletedList1.DataTextField = "MyText";***
BulletedList1.DataSource = list;
BulletedList1.DataBind();
}
}
public class Cottages{
public string MyValue { get; set; }
public string MyText { get; set; }
}

TA貢獻1828條經驗 獲得超3個贊
btnID_Click您在處理程序中有邏輯錯誤。
protected void BtnID_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBoxID.Text);
try
{
List<ASPWebApp.CottagesServiceReference.Cottages> cottages =
ws.GetCottageInfoByID(id);//.ToList(); it is List<Cottages> already
//the next line makes no sense
//ListItem cottage = new ListItem(String.Join(".", cottages));
//What should work
foreach(Cottages cottage in cottages)
{
ListItem li = new ListItem(string.Format("{0}, {1} rooms", cottage.Name, cottage.Rooms)); //add more properties of the cottage here
BulletedList1.Items.Add(li);
}
//no need
//BulletedList1.DataSource = cottages;
//BulletedList1.DataBind();
}
catch (Exception a)
{
//there is no visible console in WebForm application
//Console.WriteLine(a);
Trace.Write(a);
}
}
通常,BulletedList不是綁定復雜結構的最佳選擇。
我會推薦DataList或Repeater有ItemTemplate。
- 2 回答
- 0 關注
- 133 瀏覽
添加回答
舉報