Getting the clicked Row value of a nested repeater

Posted on 2004-11-30
Last Modified: 2012-06-21
Hi All:

Here's my repeater:

<asp:repeater id="rptDivision" runat="server">
                  <%# DataBinder.Eval(Container.DataItem, "DivisionDesc")%>
            <asp:repeater id="rptActivity" runat="server" OnItemCommand="GetActivityToEdit_ItemCommand" DataSource='<%# ActivityDataBind(DataBinder.Eval(Container.DataItem,"DivisionID")) %>'>
                        <ASP:Button ID="lblActivityID" Text='<%# DataBinder.Eval(Container.DataItem, "ActivityID") %>' runat="server">
                        <%# DataBinder.Eval(Container.DataItem, "ActivityName")%>
            </asp:repeater><BR />

I need the value of lblActivityID for whatever button they press. I don't HAVE to use the button though.. I'd really prefer a hyperlink or a LinkButton. I tried to play with GetActivityToEdit_ItemCommand, but that doesn't work well for a nested repeater.. that and I don't know how to make it work. heh.

Question by:Mitchell_
    LVL 5

    Expert Comment

    in rptDivision:]

    Sub Item_Created(ByVal sender As Object, ByVal e As DataListItemEventArgs)
       Dim neededLabel As Label = CType(e.Item.FindControl("lblActivityID"), Label)
    End Sub

    Author Comment


    I get this error:

    Compiler Error Message: BC30408: Method 'Public Sub Item_Created(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs)' does not have the same signature as delegate 'Delegate Sub RepeaterItemEventHandler(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)'.

    in the Sub that you gave me, it says "DataListItemEventArgs" - should that be that way for a repeater?

    I am also not sure I see how it will give me the value of the clicked button ..

    LVL 8

    Accepted Solution

    Sample Working Code :

    'HTML Page Code:

    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="">
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
          <asp:Repeater id="Repeater1" runat="server">
                <table border="1">
                <tr bgcolor="#ffcc99">
                <th>First Name</th>
                <th>Last Name</th>
                <tr bgcolor="#ffcccc">
                <td><asp:Button CommandName="ID" Text=<%# DataBinder.Eval(Container.DataItem, "au_id") %>
                                        runat=server ID="Button1"/></td>

                <td><%# DataBinder.Eval(Container.DataItem, "au_fname") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "au_lname") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "address") %></td>
                <tr bgcolor="#ccff99">
                <td><asp:Button CommandName="ID" Text=<%# DataBinder.Eval(Container.DataItem, "au_id") %>
                                        runat=server ID="Button2"/></td>
                <td><%# DataBinder.Eval(Container.DataItem, "au_fname") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "au_lname") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "address") %></td>


    'Code Behind

    Imports System.Data.SqlClient

    Public Class ASPRepeater
        Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
        End Sub

    #End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here

            If Not IsPostBack Then
                Dim sqlCon As New SqlConnection("Password=test;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Data Source=(local)")

                Dim ds As New DataSet
                Dim SqlDataAdapter1 As SqlDataAdapter
                Dim sqlCommand1 As SqlCommand
                Dim strSQLQuery As String = "SELECT * FROM Authors"

                sqlCommand1 = New SqlCommand(strSQLQuery, sqlCon)
                SqlDataAdapter1 = New SqlDataAdapter(sqlCommand1)

                SqlDataAdapter1.Fill(ds, "employee_record")
                Repeater1.DataSource = ds

            End If

        End Sub

        Private Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand

            If e.CommandName = "ID" Then
                Response.Write("ID selected is " & _
                    CType(e.CommandSource, Button).Text())
            End If

        End Sub
    End Class


    Good Luck.

    Author Comment


    That does work.. now I am wondering, is there any way I could adopt that to have different text on the button? like the word "Edit" and have it check a hidden label for the value?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    732 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

    18 Experts available now in Live!

    Get 1:1 Help Now