簡體   English   中英

如何在Web窗體上使用WCF服務並使用返回的List填充BulletedList <T> 來自WCF?

[英]How to consume WCF Service on Web Form and populate BulletedList with the returned List<T> from WCF?

我正在嘗試在網頁上提供服務,但項目符號列表打印"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>

斷點顯示Cottage信息正從ws.GetCottageInfoByID方法傳遞到List<ASPWebApp.CottagesServiceReference.Cottages>

為什么之后沒有打印到項目符號列表?

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");

現在看起來非常簡單,給了我很多麻煩......

您在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不是綁定復雜結構的最佳選擇。
我建議使用具有ItemTemplate DataListRepeater

這是關於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; }
}

結果。 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM