我正在尝试在网页上提供服务,但是项目符号列表可以打印"ASPWebApp.CottagesServiceReference.Cottages"
或System.collections.Generic.List 1
。显然,我希望它显示从服务上的选择查询中获取的项目。
protected void BtnID_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBoxID.Text);
try
{
List<ASPWebApp.CottagesServiceReference.Cottages> cottages = ws.GetCottageInfoByID(id).ToList();
ListItem cottage = new ListItem(String.Join(".", cottages));
BulletedList1.Items.Add(cottage);
BulletedList1.DataSource = cottages;
BulletedList1.DataBind();
}
catch (Exception a)
{
Console.WriteLine(a);
}
}
服务
public List<Cottages> GetCottageInfoByID(int id)
{
List<Cottages> cottage = new List<Cottages>();
SqlConnection conn = new SqlConnection(dataSource);
string sqlQuerySelectCottageInfo = "SELECT Cottage_Name as 'Name', Cottage_Location as Location, No_Of_Rooms as Rooms, Description, Cost_Per_Night as Cost FROM dbo.Cottages where Cottage_ID = @id";
SqlCommand cmd = new SqlCommand(sqlQuerySelectCottageInfo);
cmd.Parameters.AddWithValue("@id", id);
conn.Open();
cmd.Connection = conn;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (!reader.HasRows)
{
throw new Exception("No Cotteges Found");
}
else
{
cottage.Add(new Cottages()
{
Name = (reader[("Name")].ToString()),
Location = (reader[("Location")].ToString()),
Rooms = Convert.ToInt32(reader[("Rooms")]),
Cost = Convert.ToDecimal(reader[("Cost")]),
Description = (reader[("Description")].ToString()),
});
}
}
reader.Close();
conn.Close();
return cottage;
}
HTML
<td class="Column2" colspan="1">
<asp:TextBox class="TxtID" ID="TextBoxID" runat="server" BorderColor="Gray" BorderStyle="Solid" BorderWidth="2px" CausesValidation="False"></asp:TextBox>
<asp:Button class="BtnID" ID="BtnID" runat="server" Text="Search" OnClick="BtnID_Click" />
<asp:BulletedList class="Bullets" ID="BulletedList1" runat="server">
</asp:BulletedList>
</td>
断点显示,小屋信息正从List<ASPWebApp.CottagesServiceReference.Cottages>
方法传递到ws.GetCottageInfoByID
。
为什么之后没有打印到项目符号列表?
TIA!
编辑**
使用这种方法工作:
CottagesServiceReference.Cottages cottages = ws.GetCottageInfoByID(id);
//Populate bulleted list with Cottages class
BulletedList1.Items.Clear();
BulletedList1.Items.Add(cottages.Name);
BulletedList1.Items.Add(cottages.Location);
BulletedList1.Items.Add(cottages.Rooms.ToString() + " Rooms");
BulletedList1.Items.Add(cottages.Description);
BulletedList1.Items.Add("£" + cottages.Cost.ToString() + ".00");
现在看起来非常简单,给了我很多麻烦......
这是关于Web表单的问题,当您想在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; }
}
你在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
。