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 关注
- 108 浏览
添加回答
举报