Solved

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now