将数据列表链接按钮的文本值返回到标签中

时间:2018-10-21 04:29:41

标签: asp.net webforms

我有一个带有链接按钮的数据列表,其中显示了仓库的地点编号,单击此数据列表链接按钮时,我需要选择的位置编号才能显示在标签中。我正在努力实现这一目标;这是enter code here我的代码:

    <asp:DataList ID="LocList" runat="server" RepeatDirection="vertical" 
RepeatColumns="18" RepeatLayout="Table" OnItemDataBound="DataList_ItemDataBound"> 
<ItemTemplate>
<asp:LinkButton ID="lkLoc" runat="server" Text='<%# Eval("WHLOC") %>'  ForeColor="#000" Font-Names="calibri" Font-Size="Medium" onclick="lk_Click" />
    </ItemTemplate>
</asp:DataList>
<asp:Label ID="lbl1" runat="server"></asp:Label>

    Protected Sub lk_Click(ByVal sender As Object, ByVal e As EventArgs)
    For Each dli As DataListItem In LocList.Items
        Dim LkVal As String = CType(dli.FindControl("WHLOC"), LinkButton).Text.ToString
        lbl1.Text = LkVal.ToString
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码将为您提供帮助:

.aspx代码:

  <asp:DataList ID="LocList" runat="server" RepeatDirection="vertical" 
RepeatColumns="18" RepeatLayout="Table" OnItemDataBound="DataList_ItemDataBound" OnItemCommand="DataList_ItemCommand"> 
<ItemTemplate>
<asp:LinkButton ID="lkLoc" runat="server" Text='<%# Eval("WHLOC") %>'  ForeColor="#000" Font-Names="calibri" Font-Size="Medium" onclick="lk_Click" />
    </ItemTemplate>
</asp:DataList>
<asp:Label ID="lbl1" runat="server"></asp:Label>

.aspx.cs代码在这里:

   Protected Sub DataList_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)

        Dim lkLoc As LinkButton = CType(e.Item.FindControl("lkLoc"), LinkButton)
        lbl1.Text = lkLoc.Text

    End Sub