Solved

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

Posted on 2015-02-09
2
85 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:Mike Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Mike Miller
ID: 40600015
Thanks!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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