Solved

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

Posted on 2015-02-09
2
84 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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