如何在2个中继器内调用标签

时间:2015-04-12 04:30:52

标签: c# asp.net

我有一些代码

<div class="profile-stories">
                <asp:Repeater ID="repeatMessage" runat="server" OnItemDataBound="repeatMessage_ItemDataBound">
                    <ItemTemplate>
                        <article class="story">

                            <aside class="user-thumb">
                                <a href="#">
                                    <img src="assets/images/thumb-1.png" alt="" class="img-circle" />
                                </a>
                            </aside>

                            <div class="story-content">

                                <!-- story header -->
                                <header>

                                    <div class="publisher">
                                        <a href="#">Art Ramadani</a> posted a status update
                                        <asp:Label ID="studentName" runat="server"></asp:Label>
                                        <asp:Label ID="studentID" runat="server"></asp:Label>
                                        <asp:Label ID="msgID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Mess_Id")%>'></asp:Label>

                                        <em><%#DataBinder.Eval(Container.DataItem, "SentDate") %></em>
                                    </div>

                                    <div class="story-type">
                                        <i class="entypo-feather"></i>
                                    </div>

                                </header>

                                <div class="story-main-content">
                                    <p><%#DataBinder.Eval(Container.DataItem, "Message_content")%></p>
                                </div>


                                        <footer>

                                            <!-- story comments -->
                                            <ul class="comments">
                                                <asp:Repeater ID="repeatComment" runat="server">
                                    <ItemTemplate>
                                                <li>
                                                    <div class="user-comment-thumb">
                                                        <img src="assets/images/thumb-1.png" alt="" class="img-circle" width="30" />
                                                    </div>

                                                    <div class="user-comment-content">

                                                        <%--<a href="#" class="user-comment-name">Harold Bella</a>--%>
                                                        <asp:Label ID="userComment" CssClass="user-comment-name"></asp:Label>
                                                        <%#DataBinder.Eval(Container.DataItem, "Comment_content")%>


                                    <div class="user-comment-meta">

                                        <a href="#" class="comment-date"><%#DataBinder.Eval(Container.DataItem, "SentDate")%></a>

                                    </div>

                                                    </div>
                                                </li>
                                         </ItemTemplate>
                                </asp:Repeater>
                                                <li class="comment-form">
                                                    <div class="user-comment-thumb">
                                                        <img src="assets/images/thumb-1.png" alt="" class="img-circle" width="30" />
                                                    </div>

                                                    <div class="user-comment-content" runat="server">

                                                        <asp:TextBox ID="txtComment" CssClass="form-control autogrow" TextMode="MultiLine" runat="server"></asp:TextBox>
                                                        <asp:LinkButton ID="btnComment" CssClass="btn" Text='<i class="entypo-chat"></i>' runat="server" OnClick="btnComment_Click"></asp:LinkButton>
                                                    </div>
                                                </li>

                                            </ul>

                                        </footer>

                                <!-- separator -->
                                <hr />

                            </div>

                        </article>
                    </ItemTemplate>
                </asp:Repeater>

            </div>

现在我想在转发器repeatComment中调用Lable userComment。我做的每一件事,但我可以这样做,我调试并接收userComment = null虽然我使用repeaterComment.FindControl(&#34; userComment&#34;),但它无法正常工作。我该如何解决这个问题

1 个答案:

答案 0 :(得分:2)

您无法直接访问它,因为没有一个userComment标签,但每个数据项都有一个。

您可以从ItemDataBound事件处理程序访问转发器项模板中定义的控件。

首先你需要定义它:

<asp:Repeater ID="repeatComment" runat="server" OnItemDataBound="repeatComment_ItemDataBound">

然后在你的代码后面你可以做这样的事情:

protected void repeatComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var userComment = e.Item.FindControl("userComment") as Label;
        }
    }

我希望这可以提供帮助。