Solved

How do I get the value of a label on a repeater?

Posted on 2015-02-09
2
81 Views
Last Modified: 2015-02-09
I have a repeater with a label and a button (with OnClick="SaveCN")
In the saveCN function, how do I get the value of what is in the label?
0
Comment
Question by:mwmiller78
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40599913
In your button click function use this code:
Dim lbl As Label = sender.Parent.FindControl("Label1")

Open in new window


Here's an example page:
<%@ Page Language="vb" AutoEventWireup="false" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> EE Q_28613082 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        clicked: <asp:Label ID="LabelClicked" runat="server" Text="-"></asp:Label>
        <br />
        <br />
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# eval("name") %>'></asp:Label>
                <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# eval("id") %>' />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="SaveCN" UseSubmitBehavior="false" />
                <br />
                <br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dt As New DataTable
            dt.Columns.AddRange({New DataColumn("id", GetType(Integer)), New DataColumn("name", GetType(String))})
            For i As Integer = 1 To 10
                dt.Rows.Add({i, "Name " & i})
            Next
            Repeater1.DataSource = dt
            Repeater1.DataBind()
        End If
    End Sub

    Protected Sub SaveCN(sender As Object, e As EventArgs)
        Dim lbl As Label = sender.Parent.FindControl("Label1")
        LabelClicked.Text = lbl.Text
    End Sub

</script>

Open in new window

0
 

Author Closing Comment

by:mwmiller78
ID: 40600015
Thanks!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question