如何在 Web 窗体上使用 WCF 服务并使用 WCF 返回的 List<T> 填充

我正在尝试在网页上使用服务,但项目符号列表打印"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;

    }


GCT1015
浏览 110回答 2
2回答

江户川乱折腾

这是关于 web form 的问题,当你想在 bulletedlist 中显示自定义类的属性时,你应该将 bulletedlist 的 DataTextField 设置为该属性的名称。&nbsp; protected void Page_Load(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!IsPostBack)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Cottages> list = new List<Cottages>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.Add(new Cottages { MyText = "text1", MyValue = "value1" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.Add(new Cottages { MyText = "text2", MyValue = "value2" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.Add(new Cottages { MyText = "text3", MyValue = "value3" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;***BulletedList1.DataTextField = "MyText";***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BulletedList1.DataSource = list;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BulletedList1.DataBind();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }public class Cottages{&nbsp; &nbsp; public string MyValue { get; set; }&nbsp; &nbsp; public string MyText { get; set; }}

倚天杖

btnID_Click您在处理程序中有逻辑错误。protected void BtnID_Click(object sender, EventArgs e){&nbsp; &nbsp; int id = Convert.ToInt32(TextBoxID.Text);&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<ASPWebApp.CottagesServiceReference.Cottages> cottages =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ws.GetCottageInfoByID(id);//.ToList(); it is List<Cottages> already&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //the next line makes no sense&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //ListItem cottage = new ListItem(String.Join(".", cottages));&nbsp; &nbsp; &nbsp; &nbsp; //What should work&nbsp; &nbsp; &nbsp; &nbsp; foreach(Cottages cottage in cottages)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListItem li = new ListItem(string.Format("{0}, {1} rooms", cottage.Name, cottage.Rooms)); //add more properties of the cottage here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BulletedList1.Items.Add(li);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //no need&nbsp; &nbsp; &nbsp; &nbsp; //BulletedList1.DataSource = cottages;&nbsp; &nbsp; &nbsp; &nbsp; //BulletedList1.DataBind();&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception a)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //there is no visible console in WebForm application&nbsp; &nbsp; &nbsp; &nbsp; //Console.WriteLine(a);&nbsp; &nbsp; &nbsp; &nbsp;Trace.Write(a);&nbsp; &nbsp; }}通常,BulletedList不是绑定复杂结构的最佳选择。我会推荐DataList或Repeater有ItemTemplate。
打开App,查看更多内容
随时随地看视频慕课网APP