Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 758
  • Last Modified:

ASP.NET(VB) accessing controls in a GridView Row

I'm trying to access controls inside a Gridview. It does not like this line:
Dim repeater As Repeater = GridView1.SelectedRow.FindControl("Repeater1")
I suppose because it's not a selected row. What should it be? Here's the entire code...

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        For Each GridViewRow In GridView1.Rows
            Dim repeater As Repeater = GridView1.SelectedRow.FindControl("Repeater1")
            For Each item In repeater.Items
                Dim separator As TextBox = item.findcontrol("tbSeparator")
                Dim sepList As ListBox = item.findcontrol("lbSeparator")
                If sepList.Items.Contains(item.separator.Text) Then
                     'do something
                Else
                   
                End If
            Next
        Next
    End Sub

Open in new window


How do I access the controls inside the GridView Row?
0
Mike Miller
Asked:
Mike Miller
  • 2
1 Solution
 
Jitendra PatilSr.Software EngineerCommented:
change the code as below

For Each row As GridViewRow In GridView1.Rows

 Dim repeater As Repeater = row.FindControl("Repeater1")

 For Each item In repeater.Items
                Dim separator As TextBox = item.findcontrol("tbSeparator")
                Dim sepList As ListBox = item.findcontrol("lbSeparator")
                If sepList.Items.Contains(item.separator.Text) Then
                     'do something
                Else
                   
                End If
            Next
     Next

hope this helps.
0
 
Mike MillerSoftware EngineerAuthor Commented:
Right direction. It's not liking this line now:

Dim sepList As ListBox = item.findcontrol("lbSeparator")

Open in new window


Returning: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.ListItem'
0
 
Jitendra PatilSr.Software EngineerCommented:
its because you are trying to access item value of a list box,

first try to find  the control within the foreach loop and if the control is of type listbox typecast the same in a listbox object.

you must find the control inside of repeater and cast the control to listbox again, to get the selected value you need to again loop through the items of listview. or something like this approach could do the trick.

hope this helps.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now