如何在中继器中访问标签

Posted

技术标签:

【中文标题】如何在中继器中访问标签【英文标题】:How Can Access Label In Repeater 【发布时间】:2017-09-04 13:29:05 【问题描述】:

我尝试访问中继器中的标签以获取值并在同一标签中设置另一个值

  <ul class="mail-list">
                                        <asp:Repeater ID="repMsg" runat="server" DataSourceID="EDSrepMSg">

                                            <ItemTemplate>
                                                <li>
                                                    <asp:LinkButton ID="lnkMsgDisplay" runat="server" CausesValidation="false" CommandName="Display_Msg" CommandArgument='<%# Bind("message_id") %>'>
                                                        <span class="mail-sender">
                                                            <asp:Label ID="lblEmail" runat="server" Text='<%#Eval("message_from") %>'></asp:Label></span>
                                                        <span class="mail-subject">
                                                            <asp:Label ID="lblSubject" runat="server" Text='<%#Eval("message_title") %>'></asp:Label></span>
                                                        <span class="mail-message-preview">
                                                            <asp:Label ID="lblMsg" runat="server" Text='<%#If (Eval("message_description").ToString.Length > 10,Eval("message_description").ToString.Substring(0,10).TrimEnd() & "..",Eval("message_description"))%>'></asp:Label></span>

                                                    </asp:LinkButton>

                                                </li>
                                            </ItemTemplate>
                                        </asp:Repeater>
                                        <asp:EntityDataSource ID="EDSrepMSg" runat="server" ConnectionString="name=BWJO_dbEntities" DefaultContainerName="BWJO_dbEntities" EnableFlattening="False" EntitySetName="tbl_message"
                                            Where="it.message_to=@message_to and it.message_type=2 or it.message_type=4 " OrderBy="it.message_date desc">
                                            <WhereParameters>
                                                <asp:SessionParameter SessionField="user_id" Name="message_to" Type="Int32" DefaultValue="0" />

                                            </WhereParameters>

                                        </asp:EntityDataSource>

输出:

注意:

tbl_msg,tbl_user,tbl_emp 之间没有关系

我只想知道如何从中获得价值

<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("message_from") %>'></asp:Label>

然后将值设置为 lblEmail(编辑标签)

【问题讨论】:

【参考方案1】:

感谢 Usman Mirza 给我提示,我这样解决了:

*在repeater中创建新标签以读取message_id

<asp:Label ID="lblMsgID" runat="server" Text='<%#Eval("message_id") %>' Visible="false"></asp:Label>

<asp:Label ID="lblEmail" runat="server" Text=""></asp:Label></span>

后面的代码:

Protected Sub repMsg_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repMsg.ItemDataBound

        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim MsgID As Integer = Integer.Parse(TryCast(e.Item.FindControl("lblMsgID"), Label).Text)


            Using context As New BWJO_dbModel.BWJO_dbEntities
                Dim obj = context.tbl_message.FirstOrDefault(Function(u) u.message_id = MsgID)
                Dim fromID = obj.message_from
                Dim ToID = obj.message_to
                Dim msgtype = obj.message_type
                If msgtype = 2 Then
                    Dim userObj = context.tbl_user.FirstOrDefault(Function(u) u.user_id = fromID)
                    Dim userEmail = userObj.user_email
                    Dim lblEmail = TryCast(e.Item.FindControl("lblEmail"), Label)
                    lblEmail.Text = userEmail

                ElseIf msgtype = 4 Then
                    Dim empObj = context.tbl_employee.FirstOrDefault(Function(u) u.emp_id = fromID)
                    Dim empEmail = empObj.emp_email

                    Dim lblEmail = TryCast(e.Item.FindControl("lblEmail"), Label)
                    lblEmail.Text = empEmail

                End If

            End Using



        End If

注意:

消息类型:

           1)user-user

           2)user-role

           3)role-user

           4)role-role

【讨论】:

【参考方案2】:

你可以先得到标签,然后是它的值,像这样.. 例如你有标签:

<asp:Label ID="lblval" runat="server" Text="this is test"></asp:Label>

然后在后端你可以得到如下文本:

protected void rptrMaylike_ItemDataBound(object sender, RepeaterItemEventArgs e)

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    
        Label lblv = (Label)e.Item.FindControl("lblval");
        lblv.Text = "Value from code behind";
    

【讨论】:

以上是关于如何在中继器中访问标签的主要内容,如果未能解决你的问题,请参考以下文章

如何正确命名和访问使用中继器加载的元素 ID?

Arraylist 作为中继器数据源(如何从 .aspx 访问)

Timber/Twig - 如何访问组内中继器内的 Wordpress Post 对象

中继缓存如何工作?

Axure RP 9 中继器原理——添加行

Axure中继器的使用