如何绑定嵌套的中继器?

时间:2013-04-24 14:51:48

标签: c# asp.net ado.net

我有一个服务页面,其中显示了服务详细信息,并且包含服务功能和费用等子部分。我为功能和收费创建了两个中继器。有关更多信息,请参阅以下sql表图像:

enter image description here

但我不知道如何将此分层数据绑定到嵌套转发器。这是我的代码:

 <!--Nested Repeater Section-->
    <asp:Repeater ID="ServiceDetailsRepeater" runat="server" 
        onitemdatabound="ServiceDetailsRepeater_ItemDataBound">
    <ItemTemplate>
        <asp:HiddenField ID="service_id" runat="server" Value='<%#Eval("Id")%>'/>
    <h3><a name='<%#Eval("Id")%>'></a><%#Eval("Service_name")%></h3>
    <img src='<%# "images/"+ Eval("ImageName")%>' alt="picture" width="48" height="48" class="img" />
    <p><%#Eval("Service_desc")%></p>
    <div id="Div1" class="clr" runat="server">&nbsp;</div>
     <div id="Div2" class="box" runat="server">
        <div id="Div3" class="box_t" runat="server">
            <asp:Panel ID="Panel_ServiceFeature" runat="server" class="box_b">
        <h2>Features</h2>
        <ul style="font-family:Arial, Helvetica, sans-serif;font-size:11px;color:#5f5f5f">
            <asp:Repeater ID="ServiceFeatureRepeater" runat="server">
            <ItemTemplate>
            <li><%#Eval("Feature_Text")%></li>
            </ItemTemplate>
            </asp:Repeater>
            </ul>
            </asp:Panel>
    </div>
  </div>
   <div id="Div5" class="clr" runat="server">&nbsp;</div>
   <div id="Div6" class="box" runat="server">
        <div id="Div7" class="box_t" runat="server">
            <asp:Panel ID="Panel_ServiceCharge" runat="server" class="box_b">
        <h2>Charges</h2>
        <table style="font-family:Arial, Helvetica, sans-serif;font-size:11px;color:#5f5f5f">
        <tr><td></td>
        <td>Rs.</td><td>&nbsp;</td><td align="left">Months</td></tr>
        <asp:Repeater ID="ServiceChargeRepeater" runat="server">
        <ItemTemplate>
        <tr>
        <td><img src="images/sub_nav.gif" width="18px" height="14px" alt=""/></td>
        <td>Rs. <%#String.Format("{0:#,###}", Eval("Charges_Per_Month"))%></td>
        <td>&nbsp;</td>
        <td><%#Eval("No_Of_Month")%> Months</td>
        </tr>
            </ItemTemplate>
            </asp:Repeater>
        </table>
            </asp:Panel>
  </div>
 </div>
 <div id="Div9" class="clr" runat="server"></div>
    </ItemTemplate>
    </asp:Repeater>
  <!--End Section--> 

最后,my web site page这是我的实时网站服务页面视图。此页面是静态的,但现在我想将此服务详细信息与数据库绑定。

2 个答案:

答案 0 :(得分:2)

一种方法是简单地将内部DataSource的{​​{1}}属性绑定到合适的集合。例如。类似的东西:

Repeater
  

我还检查是否找不到特定服务的功能或费用,然后面板必须关闭。所以我直接从代码背后做

<强>更新

如果你更乐于从代码隐藏中继续操作,那么你也可以通过数据绑定Repeater的 <asp:Repeater ID="ServiceDetailsRepeater" runat="server" DataSourceID="SqlDataSource1"> <asp:Repeater ID="ServiceFeatureRepeater" runat="server" DataSource="<%# DatabaseHelper.GetFeatures(Eval("Id")) %>"> ... </asp:Repeater> <asp:Repeater ID="ChargesRepeater" runat="server" DataSource="<%# DatabaseHelper.GetCharges(Eval("Id")) %>"> ... </asp:Repeater> </asp:Repeater> 属性来声明性地做到这一点,例如:

Visible

答案 1 :(得分:1)

if (!Page.IsPostBack)
            {
                ServiceListRepeater.DataSource = DatabaseHelper.GetSummaryOfActiveServices();
                ServiceListRepeater.DataBind();
                ServiceDetailsRepeater.DataSource = DatabaseHelper.GetSummaryOfActiveServices();
                ServiceDetailsRepeater.DataBind();
            }

protected void ServiceDetailsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if ((item.ItemType == ListItemType.Item) ||(item.ItemType == ListItemType.AlternatingItem))
        {
            HiddenField service_id = (HiddenField)item.FindControl("service_id");
            Repeater service_features = (Repeater)item.FindControl("ServiceFeatureRepeater");
            service_features.DataSource = DatabaseHelper.GetFeatureSummary(int.Parse(service_id.Value.ToString()));
            service_features.DataBind();
            if (service_features.Items.Count == 0)
            { 
                Panel Panel_ServiceFeature = (Panel)item.FindControl("Panel_ServiceFeature");
                Panel_ServiceFeature.Visible = false;
            }
            Repeater service_charges =(Repeater)item.FindControl("ServiceChargeRepeater");
            service_charges.DataSource = DatabaseHelper.GetChargeSummary(int.Parse(service_id.Value.ToString()));
            service_charges.DataBind();
            if (service_charges.Items.Count == 0)
            {
                Panel Panel_ServiceCharge = (Panel)item.FindControl("Panel_ServiceCharge");
                Panel_ServiceCharge.Visible = false;
            }
        }
    }