Asp.net Repeater with Gridview

Joseph Mancini 81 Reputation points
2021-06-04T19:50:32.323+00:00

I have a Repeater Control, and within the Repeater, I have a bunch of text boxes and a GridView. I have a button in the Gridview and when the user clicks on the button, I want to read what is in 1 of the textboxes. I can loop through the textbox items as I do below and see all instances of the text boxes, but how do I know which button in the GridView was clicked? Once I know that, then I can look at the correct text box.

            For Each item As RepeaterItem In repeaterSession.Items
                Dim txtStartDate As TextBox = CType(item.FindControl("txtStartDate"), TextBox)
            Next
Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. Albert Kallal 5,586 Reputation points
    2021-06-08T03:57:42.027+00:00

    Ok, I like this question.

    I am going to assume this:

    We have a repeater - we pull the repeating rows from SQL server (lets say Hotels)

    Then inside of the Repeater - we want to see the people booked in that hotel (lets say table People).

    So, when we click in one of the repeating groups and inside that grid, we want BOTH the grid row, and then also the repeater row also, right?

    Ok, so so lets assume this markup

    ---- Anyone here know how to paste markup on this page???

    MY VERY sorry - the markup has to be a image. But it is this:

    103272-capture2.png

    So not too bad. We have some information about the Hotel, and then we have that grid view inside showing the people booked for that repeater.

    So, the above looks like this:

    103219-capture.png

    So our code to fill this out is thus this:

        Dim MyTable As New DataTable  
      
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
            If IsPostBack = False Then  
                LoadGrid()  
            End If  
      
        End Sub  
      
        Sub LoadGrid()  
      
            Dim strSQL = "SELECT * FROM tblHotels WHERE Active = 1 ORDER BY HotelName"  
      
            Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST4))  
      
                cmdSQL.Connection.Open()  
                MyTable.Load(cmdSQL.ExecuteReader)  
                Repeater1.DataSource = MyTable  
                Repeater1.DataBind()  
      
            End Using  
      
        End Sub  
      
        Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound  
      
            Select Case e.Item.ItemType.ToString  
      
                Case "Item", "AlternatingItem"  
      
                    ' create sql for grid  - INCLUDE Repeater row in SQL!!!  
      
                    ' Get PK of this row - for child pull  
      
                    Dim RepRow As Integer = e.Item.ItemIndex  
      
                    Dim HotelPK As Integer = MyTable.Rows(RepRow).Item("ID")  ' HotelPK  
      
                    Dim strSQL As String =  
                       "SELECT ID, FirstName, LastName, City," & e.Item.ItemIndex & " AS RepRow " &  
                       " from People WHERE Hotel_ID = " & HotelPK & " Order by FirstName"  
      
                    Dim MyGrid As GridView = e.Item.FindControl("GridView1")  
      
                    Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST4))  
                        cmdSQL.Connection.Open()  
                        MyGrid.DataSource = cmdSQL.ExecuteReader  
                        MyGrid.DataBind()  
                    End Using  
      
                Case "Footer"  
      
            End Select  
      
        End Sub  
      
      
        Protected Sub btnSel_Click(sender As Object, e As EventArgs)  
      
      
            Dim btn As Button = sender  
      
            Dim intRepeatRow As Integer = btn.Attributes("MyRepeatRow")  
            Dim intGirdRow As Integer = btn.Attributes("MyGridRow")  
      
            ' get repeater row  
            Dim RepeatData As RepeaterItem = Repeater1.Items(intRepeatRow)  
      
            Dim strHotel As String = TryCast(RepeatData.FindControl("txtHotelName"), Label).Text  
      
            ' get grid from that repeater row  
            Dim myGrid As GridView = TryCast(RepeatData.FindControl("GridView1"), GridView)  
            Dim myGridRow As GridViewRow = myGrid.Rows(intGirdRow)  
      
            Dim strFirstNameFromGrid = TryCast(myGridRow.FindControl("txtFirst"), Label).Text  
      
            Debug.Print("Hotel Name (repeater) = " & strHotel)  
            Debug.Print("First Name from Grid = " & strFirstNameFromGrid)  
      
        End Sub  
      
    

    So, when we click on a row, the output is this:

    Hotel Name (repeater) = Jasper Park Lodge
    First Name from Grid = Scott

    Note how by adding the extra attributes to the button - we don't even care about or deal with the repeater events - it just a button click - we get the two rows (repeat row, and grid row).

    So look VERY VERY close how I added some custom attributes to the button markup in that screen cap.

    Regards,

    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada


1 additional answer

Sort by: Most helpful
  1. Joseph Mancini 81 Reputation points
    2021-06-07T16:55:32.777+00:00

    Let me explain/clarify a bit - Because I am using a repeater, there may be multiple textboxes (that are outside of the gridview), as well as multiple gridview controls. I know I can capture the click event handler of the button that was clicked in the GridView that is in the Repeater. How do I best determine the Repeater name where the button that was clicked in the GridView?

    I did try this code below on the click event of the button and when parsing stuff out, I think I can get the Repeater id to get the text box I need to get. Still testing with multiple repeater rows, but maybe there is a better(correct?) way?

                Dim repeaterid As Integer
                'Get the button that raised the event
                Dim btn As Button = CType(sender, Button)
                'the last number in ControlID contains the index of the repeater that we want to read the boxes from
                Dim controlId As String = btn.ClientID
                'Parse the grid name/number out of controlid.
                controlId = Replace(controlId, "repeaterSession_grdUserList_", "")
            'Get the Index of the Repeater we are on so we can grab data from the right section
                repeaterid = CInt(Mid(controlId, 1, 1))
    
    
            For Each item As RepeaterItem In repeaterSession.Items
                Dim txtStartDate As TextBox = CType(item.FindControl("txtStartDate"), TextBox)
                If Mid(txtStartDate.ClientID, Len(txtStartDate.ClientID), 1) = repeaterid Then
                    StartDate = txtStartDate.Text
                End If
             Loop
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.