在转发器中绑定自定义类数组

时间:2012-03-12 17:25:15

标签: c# asp.net class repeater

enter image description here

我想将CourseFee[]数组绑定到转发器。 我想在我的转发器中绑定AmountCourseFeeType.Descr。 我如何绑定它?

1 个答案:

答案 0 :(得分:2)

样本类

public class Order
{
    public CourceFeeType FeeType;
    public int Amount;
    public int CourseFee;

    public void AddFeeTypeDetails(CourceFeeType Fees)
    {
        FeeType = new CourceFeeType();
        FeeType.Code = Fees.Code;
        FeeType.Desc = Fees.Desc;
    }

    // Nested class
    public class CourceFeeType
    {
        public String Code;
        public String Desc;
    }
}

样本表格加载代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        List<Order> List = new List<Order>();

        Order OrderObj = new Order();
        Order.CourceFeeType Fees = new Order.CourceFeeType();
        Fees.Code = "1";
        Fees.Desc = "w2s";
        OrderObj.Amount = 1;
        OrderObj.AddFeeTypeDetails(Fees);

        List.Add(OrderObj);

        OrderObj = new Order();
        OrderObj.Amount = 2;
        Fees = new Order.CourceFeeType();
        Fees.Code = "2";
        Fees.Desc = "w22s";
        OrderObj.AddFeeTypeDetails(Fees);

        List.Add(OrderObj);

        rpt.DataSource = List;
        rpt.DataBind();
    }

}

转发器项目绑定数据事件代码

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Label lbl = (Label)e.Item.FindControl("lblDescription");
        lbl.Text = ((Order)e.Item.DataItem).FeeType.Desc;

        Label lblAmount = (Label)e.Item.FindControl("lblAmount");
        lblAmount.Text = ((Order)e.Item.DataItem).Amount.ToString();
    }
}

示例HTML

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
    <HeaderTemplate>
        <table>
            <tr>
                <td>
                    Amount
                </td>
                <td>
                    Description
                </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblAmount" runat="server"></asp:Label>
            </td>
            <td>
                <asp:Label ID="lblDescription" runat="server"></asp:Label>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>