【问题标题】:How can call lable inside 2 repeater如何在 2 个中继器内调用标签
【发布时间】:2015-04-12 04:30:52
【问题描述】:

我有一些代码

<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("userComment") 并且它不起作用。我该如何解决这个问题

【问题讨论】:

  • 你试过的代码在哪里?
  • 我在后面添加代码请帮帮我

标签: c# asp.net


【解决方案1】:

您无法直接访问它,因为没有一个 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;
        }
    }

我希望这会有所帮助。

【讨论】:

  • 我上面写的还不够吗?只需在 aspx 代码中添加事件处理程序定义 (OnItemDataBound="repeatComment_ItemDataBound") 并在您的代码隐藏代码中添加方法,您将看到 userComment 变量不会为 null 并且会正确引用标签
  • 这里回答了同样的问题:stackoverflow.com/questions/3626564/…
  • 我只是看到你的标签没有用runat="server" 标记。试试这个:.
  • 好 :-) 你愿意接受和/或投票给我的答案吗?
  • @trentfernandez,如果你认为答案是正确的,你应该投票。请参阅stackoverflow.com/help/someone-answersstackoverflow.com/help/why-vote
猜你喜欢
  • 2019-11-22
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多