如何以编程方式在嵌套转发器中创建标签

时间:2013-04-17 19:13:24

标签: asp.net

我有两个中继器,一个嵌套在另一个中。我正在输出父转发器中的课程标题列表以及子转发器中这些课程的日期。这部分工作正常。但是,并非所有的课程标题都必须在任何给定的时间都有可用日期,所以我希望能够在每个课程标题下放一条消息,目前没有日期说要定期检查,等等,等等,等等。我已将标签放在嵌套转发器的页脚模板中,其visibility = false,并尝试在ItemDataBound Sub中的适当时间设置visibility = true。不幸的是,这不起作用。我没有收到任何错误,标签没有出现。我真的希望有人可以帮我解决我的代码或建议另一种方法来做到这一点。我是asp.net(VB)的新手,我还在苦苦挣扎。我以前从未尝试过嵌套中继器。这是我的代码:

的.aspx

<asp:Repeater ID="rptTech" runat="server" >
            <HeaderTemplate>
                <table class="tableCourses">
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <strong><asp:Label runat="server" ID="lblCourseName" Text='<%# Eval("CourseName") %>'></asp:Label></strong>
                        <br />
                        <asp:Label runat="server" ID="lblCourseSummary" Text='<%# Eval("Summary") %>'></asp:Label>
                        <br />
                        <asp:HyperLink ID="hpCourseMaterial" Visible='<%# CheckCourseMaterial(Eval("CourseMaterial")) %>' NavigateUrl='<%# "/files/Portal_Course_Material/" & Eval("CourseMaterial")%>' Text="Download Course Material" runat="server"></asp:HyperLink>
                    </td>
                </tr>
                <tr>
                    <td>
                    <asp:Repeater ID="rptTechDates" DataSource='<%#Eval("relCourses") %>' runat="server" >
                        <HeaderTemplate>
                            <table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <strong><asp:Label runat="server" ID="lblDate" Text='<%# CDate(Eval("dteDate")).ToString("dd/MM/yyyy") %>'></asp:Label></strong><br />
                                    <asp:Label runat="server" ID="dteTime" Text='<%# "Time: " & CDate(Eval("dteTime2")).ToString("HH:mm") & " EST" %>'></asp:Label><br />
                                    <asp:HyperLink ID="Register" NavigateUrl='<%# "/Test.aspx?intMeetingID2=" & Eval("pkWebinarID")%>' Text="Register" runat="server"></asp:HyperLink>
                                </td>
                            </tr>
                        </ItemTemplate> 
                        <FooterTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="lblNoCourses" Text="There are no course dates available at this time. Please check back regularly to see any updates." runat="server" Visible="False"></asp:Label>
                                </td>
                            </tr>
                            </table>
                        </FooterTemplate>
                    </asp:Repeater>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>

和Codebehind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim curDate As Date = CDate(Now)

    'Create the connection
    Dim connstring As String
    connstring = ConfigurationManager.ConnectionStrings("LPISQLConn").ConnectionString

    Using conn As New SqlConnection(connstring)
        Dim cmd As New SqlCommand("SELECT foo1, foo2, foo3 FROM tblFoo", conn)
        cmd.CommandType = CommandType.Text

        Dim ad As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        ad.Fill(ds)
        ds.Relations.Add(New DataRelation("relCourses", ds.Tables(0).Columns("fkcrsID"), ds.Tables(1).Columns("webinarID"), False))
        rptTech.DataSource = ds.Tables(0)
        rptTech.DataBind()
    End Using
End Sub

Protected Sub rptTech_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
        Dim rptTechDates As Repeater = TryCast(e.Item.FindControl("rptTechDates"), Repeater)
        rptTechDates.DataSource = drv.CreateChildView("relCourses")
        If rptTechDates.Items.Count < 1 And rptTechDates IsNot Nothing Then
            If e.Item.ItemType = ListItemType.Footer Then
                Dim lblNoCourses As Label = TryCast(e.Item.FindControl("lblNoCourses"), Label)
                If lblNoCourses IsNot Nothing Then
                    lblNoCourses.Visible = True
                End If
            End If
        Else
            rptTechDates.DataBind()
        End If

    End If
End Sub

2 个答案:

答案 0 :(得分:0)

您想搜索rptTechDates转发器以获取lblNoCourses控件。 e.Item.FindControl(“lblNoCourses”)正在搜索不存在的rptTech转发器。请参阅此question以在rptTechDates页脚中搜索控件。

此外,您需要在设置Databind()后调用转发器上的DataSource

rptTechDates.DataSource = drv.CreateChildView("relCourses")
rptTechDates.Databind()

答案 1 :(得分:0)

我会做以下事情:

将父转发器的ItemDataBoundEvent更改为:

Protected Sub rptTech_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
  If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
      Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
      Dim rptTechDates As Repeater = TryCast(e.Item.FindControl("rptTechDates"), Repeater)
      rptTechDates.DataSource = drv.CreateChildView("relCourses")
      rptTechDates.DataBind()
  End If
End Sub

将此添加到您的子中继器的标记:

OnItemDataBound="rptTechDates_ItemDataBound"

现在添加:

Protected Sub rptTechDates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
  If DirectCast(DirectCast(e.Item.NamingContainer, Repeater).DataSource, DataTable).Rows.Count = 0 Then
    DirectCast(e.Item.FindControl("lblNoCourses"), Label).Visible = True
  End If
End If

因此,如果子转发器的数据源中的行数为0,则显示标签。

编辑在OP发表评论后

Protected Sub rptTechDates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
  If DirectCast(DirectCast(e.Item.NamingContainer, Repeater).DataSource, DataView).Count = 0 Then
    DirectCast(e.Item.FindControl("lblNoCourses"), Label).Visible = True
  End If
End If