Why can't FindControl find a button in the footer of my repeater?

I am using the OnItemDataBound event to try to activate a disabled button in a repeater. Quite simply, if the event fires, I know that there are elements in the repeater, and therefore you want to enable the button. Where I'm stuck is when I click a button in a function, so I can turn it on. The relevant part of the repeater code is given below:

<asp:Repeater ID="RptEnterHours" runat="server" DataSourceID="SQL_EmployeeGetTimesheet" ClientIDMode="Predictable" OnItemDataBound="RptEnterHours_Bound">
     '.....Irrelevant code.....
     <FooterTemplate>
          <asp:Button Enabled="false" ID="SubmitTimesheets" Text="Submit All Timesheets" OnClick="processTimesheetEntry" runat="server" OnClientClick="checkValues();" />&nbsp;
     </FooterTemplate>
</asp:Repeater>

This is my code:

Sub RptEnterHours_Bound(Sender As Object, e As RepeaterItemEventArgs)

    'Exposes the Submit All Timesheets button if timesheets are available.
    If (e.Item.ItemType = ListItemType.Item) Or _
        (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim sButton As Button = TryCast(Me.FindControl("SubmitTimesheets"), Button)
        sButton.Enabled = True
    End If

End Sub

This and all other attempts led to the terrible message, "Link to an object not set to an instance of the object." Can someone tell me what I am doing wrong and why my code cannot find the button behind?

+5
source share
6 answers

try this, i am sure it will help you.

    If e.Item.ItemType = ListItemType.Footer Then
        Dim btn as new button
        btn = CType(e.Item.FindControl("SubmitTimesheets"), Button)
        btn.enabled = true
    End If
+3

.

:

If (e.Item.ItemType = ListItemType.Item) Or _ 
        (e.Item.ItemType = ListItemType.AlternatingItem) Then 

To:

If (e.Item.ItemType = ListItemType.Footer) Then 
0

e.Item.ItemType = ListItemType.Footer. AlternatingItem , . Items AlternatingItems.

RptEnterHours.DataSource. RptEnterHours.DataSource .

, - . , , :

Sub RptEnterHours_Bound(Sender As Object, e As RepeaterItemEventArgs)

    'Exposes the Submit All Timesheets button if timesheets are available.
    If (e.Item.ItemType = ListItemType.Footer) Then
        Dim sButton As Button = TryCast(Me.FindControl("SubmitTimesheets"), Button)
        Dim myDataSource = CType(RptEnterHours.DataSource, MyDataSourceType)

        sButton.Enabled = (myDataSource.Count > 0)
    End If

End Sub
0

, -, , .

Item AlternatingItem, , . , , .

, - , , . , @codingkiwi.com, , , FindControl. Me.FindControl, 1- ( , , , Me ). , . , e.Item.FindControl.

, , , , . , , OnDataBound, , - : ( VB )

If (Me.RptEnterHours.Items IsNot Null AndAlso Me.RptEnterHours.Items.Any()) Then
0

, , , , Item/AlternatingItem:

Private m_bolEnableButton As Boolean = False

Sub RptEnterHours_Bound(Sender As Object, e As RepeaterItemEventArgs)

    'Exposes the Submit All Timesheets button if timesheets are available. 
    If (e.Item.ItemType = ListItemType.Item) Or _
       (e.Item.ItemType = ListItemType.AlternatingItem) Then

        '"if the event is triggered, I know there are items in the repeater and therefore want to enable the button"
        m_bolEnableButton = True

    End If

    If e.Item.ItemType = ListItemType.Footer Then

        If m_bolEnableButton Then

            Dim sButton As Button = TryCast(e.Item.FindControl("SubmitTimesheets"), Button)
            sButton.Enabled = True

        End If

        m_bolEnableButton = False

    End If

End Sub
0

, null, , , . FindControl . , , - . FindControl.

In addition, you must search ListItemType.Footerto refer to the footer row.

Finally, FindControl () is not recursive. It finds only controls in the top-level naming container. In most database controls, each row represents its own name container , so you must findControl in the row you want to find . When you use Meit refers to the page. Instead, you should use e.Item.FindControl ().

the code:

Dim bRecordsFound as Boolean = False

Sub RptEnterHours_Bound(Sender As Object, e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Item) Or _
            (e.Item.ItemType = ListItemType.AlternatingItem) Then
        bRecordsFound = True
    End If
    If (e.Item.ItemType = ListItemType.Footer) And (bRecordsFound) Then
        Dim sButton As Button = e.Item.FindControl("SubmitTimesheets")
        If sButton IsNot Nothing Then
            sButton.Visible = True
        End If
    End If
End Sub
0
source

All Articles