簡體   English   中英

嵌套中繼器 - 從子中繼器訪問父中繼器內的控件

[英]Nested repeaters - accessing a control inside the parent repeater from the child repeater

我有一個頁面,其中列出了幾家酒店,並且對於每家酒店,我列出了用戶選擇的每個日期的價格。

我的 HTML 看起來像這樣:

<table>
<asp:Repeater ID="rptrHotels" runat="server">
    <ItemTemplate>
        <tr><td><%# Eval("HotelName") %></td></tr>
        <tr>
            <asp:Repeater ID="rptrHotelRates" runat="server" DataSource='<%# GetHotelRates(Container.DataItem) %>'>
                <ItemTemplate>
                    <td>
                        <span class="date"><%# Eval("Date") %></span>
                        <span class="price">$<%# Eval("Price") %></span>
                    </td>
                </ItemTemplate>
            </asp:Repeater>
            <td>
                <span class="date">Total</span>
                <span class="price">$<asp:Literal ID="litTotal" runat="server" /></span>
            </td>
        </tr>                   
    </ItemTemplate>
</asp:Repeater>
</table>

我的代碼隱藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DateTime startDate = Request.QueryString["StartDate"];
        DateTime endDate = Request.QueryString["EndDate"];

        List<Hotel> hotels = GetHotels(startDate, endDate);
        rptrHotels.DataSource = hotels;
        rptrHotels.DataBind();
    }
}


protected List<Rate> GetHotelRates(object item)
{
    DateTime startDate = Request.QueryString["StartDate"];
    DateTime endDate = Request.QueryString["EndDate"];
    Hotel hotel = (Hotel)item;

    List<Rate> rates = GetRates(hotel, startDate, endDate);

    decimal total = (from r in rates
                     select r.Price).Sum();

    return rates;
}

在我的 GetHotelRates() function 中,我通過對所有房價價格求和來獲得總價格。 但是我需要以某種方式將該值放入 litTotal 中,它位於子中繼器之外,但在父中繼器內部。

我怎樣才能做到這一點?

在您的項目綁定事件中執行以下操作:

((Literal)((Repeater)sender).Parent.FindControl("litTotal")).Text;

您可能需要對父母進行另一次演員才能將其發送到中繼器,但我不確定。

您總是可以嘗試一些 NamingContainer.Parent.Parent....FindControl (誇張的)魔術,但更簡潔的方法是創建一個單獨的方法來獲得您的總數:

protected decimal GetHotelTotal(object item)
{
    Hotel hotel = (Hotel)item;

    List<Rate> rates = GetRates(hotel, startDate, endDate);

    decimal total = (from r in rates
                 select r.Price).Sum();
    return total;
}

並在您的文字中調用它:

<asp:Literal ID="litTotal" runat="server" Text="<%# GetHotelTotal(Container.DataItem) %>" />

暫無
暫無

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

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