【问题标题】:Nested repeaters - accessing a control inside the parent repeater from the child repeater嵌套中继器 - 从子中继器访问父中继器内部的控件
【发布时间】:2011-06-03 20:28:35
【问题描述】:

我有一个页面列出了几家酒店,对于每家酒店,我都会列出用户选择的每个日期的价格。

我的 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() 函数中,我通过对所有房价价格求和来获得总价格。但我需要以某种方式将该值放入 litTotal,它位于子中继器之外,但在父中继器内部。

我该怎么做?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    在您的项目绑定事件中执行以下操作:

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

    您可能需要对父级进行另一次强制转换才能将其传送到中继器,但我不确定。

    【讨论】:

    • 我的 GetHotelRates() 函数中没有 object sender 变量。我需要做什么才能添加它?
    • 只有当你有一个 ItemDataBind 事件时你才会得到它。基本上每次一个项目被数据绑定时,你都会使用上面的那行代码。
    • 我从来没有想过我可以将发件人作为容器中继器!为大量代码 +1。
    【解决方案2】:

    您总是可以尝试一些 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) %>" />
    

    【讨论】:

    • 当然,如果运行 GetRates 的成本很高,您可能需要考虑设计一些不同的方法,要么在调用中保留值,要么使其成为 Hotel 对象的一部分,以便它已经存在。
    • 我认为我实际上更喜欢第二种方法。我将向由您的 GetRates 填充的酒店添加一个属性 List HotelRates。然后你不必在 DataBinding 时搞砸它,你已经准备好一切都可以干净地使用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多