jezz jezz - 5 months ago 69
Vb.net Question

How to get the value of "Label" inside the repeater?

Hi I just wanna ask on how to get the value of Label inside the repeater. I want to pass the value of "lblProduct" on the command name ="ViewDetails". Thanks

<asp:Repeater ID="reptrData" runat="server" >
<ItemTemplate>
<div>
<h6>Product Details</h6>
<asp:Panel ID="PanelHeader" runat="server" CssClass="ui-widget-header">
<table>
<tr>
<td><b>Product:</b></td>
<td><asp:Label ID="lblProduct" runat ="server"><%#Eval("plCode")%></asp:Label>
</td>
</tr>
<tr>
<td><b>Date:</b></td>
<td><asp:Label ID="lblDate" runat ="server"><%#Eval("plDate")%></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>

Answer

You can use the FindControl method of the repeater item to find the Label:

protected void reptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    Label lblProduct = e.Item.FindControl("lblProduct") as Label;
    string product = lblProduct.Text;
}

For some reason, the text of the Label is empty in code-behind when I use your current markup. If the Text property of the Label is used to set the content, however, it can be retrieved:

<asp:Repeater ID="reptrData" runat="server" onitemcommand="reptrData_ItemCommand">
    <ItemTemplate>
        <div>
            <h6>
                Product Details</h6>
            <asp:Panel ID="PanelHeader" runat="server" CssClass="ui-widget-header">
                <table>
                    <tr>
                        <td>
                            <b>Product:</b>
                        </td>
                        <td>
                            <asp:Label ID="lblProduct" runat="server" Text='<%# Eval("plCode") %>' />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Date:</b>
                        </td>
                        <td>
                            <asp:Label ID="lblDate" runat="server" Text='<%# Eval("plDate") %>' />
                        </td>
                    </tr>
                </table>
            </asp:Panel>
        </div>
    </ItemTemplate>
</asp:Repeater>

N.B. I also added the closing tag for the div element in the ItemTemplate.

Comments