Link to home
Start Free TrialLog in
Avatar of AusBoyz
AusBoyz

asked on

FormView FindControl on TextBox Returning Nulls

I am trying to reference three textboxes inside a formview to read their values and calculate a value to assign to a Label in the same formview. When I try to references any of the objects needed using the findcontrol method of the formview, it simply returns nulls (Object reference not set to an instance of an object).


Private Sub AllowanceFormView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles AllowanceFormView.DataBound
        If AllowanceFormView.CurrentMode = FormViewMode.Edit Then
            Dim HalfYearlyCarryOverLabel As Label = AllowanceFormView.FindControl("EditHalfYearlyCarryOverLabel")
            Dim BroughtForwardTextBox As TextBox = AllowanceFormView.FindControl("BroughtForwardTextBox")
            Dim JulAmountVouchedTextBox As TextBox = AllowanceFormView.FindControl("JulAmountVouchedTextBox")
            Dim JulAllocationTextBox As TextBox = AllowanceFormView.FindControl("JulAllocationTextBox")
 
            HalfYearlyCarryOverLabel.Text = (CInt(BroughtForwardTextBox.Text) + CInt(JulAmountVouchedTextBox.Text)) - CInt(JulAllocationTextBox.Text)
        End If
End Sub

Open in new window

Avatar of David Robitaille
David Robitaille
Flag of Canada image

I did it ofen and it should work.
Does the textboxes are inside something else?
(please send some code from the aspx)
Hi! Try this one.



Private Sub AllowanceFormView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles AllowanceFormView.DataBound
        If AllowanceFormView.CurrentMode = FormViewMode.Edit Then
			For i As Integer = 0 To AllowanceFormView.items.count
			
				Dim HalfYearlyCarryOverLabel As Label = AllowanceFormView.items(i).FindControl("EditHalfYearlyCarryOverLabel")
				Dim BroughtForwardTextBox As TextBox = AllowanceFormView.items(i).FindControl("BroughtForwardTextBox")
				Dim JulAmountVouchedTextBox As TextBox = AllowanceFormView.items(i).FindControl("JulAmountVouchedTextBox")
				Dim JulAllocationTextBox As TextBox = AllowanceFormView.items(i).FindControl("JulAllocationTextBox")
			
			Next
		 
            HalfYearlyCarryOverLabel.Text = (CInt(BroughtForwardTextBox.Text) + CInt(JulAmountVouchedTextBox.Text)) - CInt(JulAllocationTextBox.Text)
        End If
End Sub

Open in new window

drypz:, i see what you are trying to do, but there is some bugs in this algorithm.
the object will be set back nothing if the correct item is not the last one and they are declared inside the for-next loop, so they wont be accessible outside it.
but this could be help to troubleshoot and find the conintainer of the textboxs...
Avatar of AusBoyz
AusBoyz

ASKER

davrob60:

The TextBoxes are inside a FormView, and only inside an EditItemTemplate. Nothing else.



If I add the supplied code above to a Button's onClick event, the code works just fine. So I am able to reference them within a Button onClick event, just not the FormView DataBound event. This is unfortunately as I would like to calculate these fields automatically, and not require the user to click a Button to calculate them.
ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AusBoyz

ASKER

The CType() was it!

Thank you very much for your help davrob!