Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through a linkbuttons in repeater? 1

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
Hi Everyone,

I've created a repeater with linkbuttons that displays a through z. If you click on a link in the repeater a datagrid show only the record accounts that start with that letter. My problem now is I don't want all the links to be enabled if there are only record accounts that start with a, b, and c. I want to disable all of the ones that aren't available.


I'm getting an error on this line:

For Each linkButtonsInRepeater In rptLetters.Items

This is the exception:

System.InvalidCastException: Specified cast is not valid.

Can someone help?

THANKS!!!!

Code:
    Private Function checkForExistance(ByVal dt As DataTable)

        Dim row As DataRow
        Dim letter As String
        Dim linkButtonsInRepeater As LinkButton = CType(rptLetters.FindControl("lnkLetter"), LinkButton)

        'looping through a datatable
        For Each row In dt.Rows
            'Getting the first letter of column text
            letter = CType(row(0), String)
            letter = letter.Substring(0, 1)

            ' looping through the linkbuttons in repeater
            For Each linkButtonsInRepeater In rptLetters.Items

                If linkButtonsInRepeater.Text = letter Then
                    linkButtonsInRepeater.Enabled = True
                End If
            Next
        Next

    End Function


    Private Sub rptLetters_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptLetters.ItemCreated
        Dim letter As LinkButton = CType(e.Item.FindControl("lnkLetter"), LinkButton)
        letter.Enabled = False
    End Sub
 
If you have a look what the rptLetters.Items method is expecting back you you find that it is a System.Web.UI.WebControls.RepeaterItem.

Obviously a LinkButton is not what it is expectin and therfore cant cast it.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
what is the solution?

I'm totally clueless. I was logically thinking --loop through all the 'items' in the repeater with are linkbuttons--.

Thanks!
 
If you read what I said in my last post you should be able to work out how to do it. The Items method is expecting a RepeaterItem therefore:
Code:
Dim rptItem As [b]RepeaterItem[/b]
For Each rptItem In Repeater1.Items
	' Set the index of the control below to the index where your linkbutton is
	rptItem.Controls(1).Visible = True
Next
[code]

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ([URL unfurl="true"]http://www.tek-tips.com/search.cfm)[/URL] or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top