亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
GCT1015 2022-12-31 13:54:09
我正在嘗試在網頁上使用服務,但項目符號列表打印"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;    }
查看完整描述

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; }

}


查看完整回答
反對 回復 2022-12-31
?
倚天杖

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。


查看完整回答
反對 回復 2022-12-31
  • 2 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號