0

我有一个服务页面,它显示服务详细信息,并有一个像服务功能和费用这样的子部分。我为功能和费用创建了两个中继器。有关详细信息,请参阅以下 sql 表图像:

在此处输入图像描述

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

 <!--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--> 

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

4

2 回答 2

2

一种方法是简单地将DataSource内部的属性绑定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>

我还检查了是否没有发现特定服务的功能或费用,然后面板必须关闭。所以我直接从后面的代码做

更新

如果您更愿意从代码隐藏中执行此操作,请继续,但您也可以通过数据绑定 Repeater 的Visible属性以声明方式执行此操作,例如:

    ...
     <asp:Repeater ID="ServiceFeatureRepeater" runat="server" 
        DataSource="<%# DatabaseHelper.GetFeatures(Eval("Id")) %>"
        Visible="<%# DatabaseHelper.HasFeatures(Eval("Id")) %>"
        >
        ...
     </asp:Repeater>
    ....
于 2013-05-29T14:47:15.650 回答
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;
            }
        }
    }
于 2013-04-29T22:27:50.617 回答