?
Solved

FormView FindControl on TextBox Returning Nulls

Posted on 2009-07-01
6
Medium Priority
?
739 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:AusBoyz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24757872
I did it ofen and it should work.
Does the textboxes are inside something else?
(please send some code from the aspx)
0
 
LVL 13

Expert Comment

by:drypz
ID: 24760834
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

0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 24762673
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...
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:AusBoyz
ID: 24803524
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.
0
 
LVL 18

Accepted Solution

by:
David Robitaille earned 1000 total points
ID: 24803678
OK, then it should work, as i said, i got code like that that run smoothly.
I included some of my code just to prove you that it should work. (i cast them with CType, but it should not change anything)
The only thing i could say is to double check your ID, you may have mistyped "JulAllocationTextBox" or any others.
on other thing, does any of those textbox is dynamically created?

 Protected Sub TemplateFormView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles TemplateFormView.DataBound
 
        '****show the insert or update button as the form mode***
        If TemplateFormView.CurrentMode = FormViewMode.Edit Or _
            TemplateFormView.CurrentMode = FormViewMode.Insert Then
            'get the buttons
            Dim InsertButtonTop As LinkButton = CType(TemplateFormView.FindControl("InsertButtonTop"), LinkButton)
            Dim InsertButtonBottom As LinkButton = CType(TemplateFormView.FindControl("InsertButtonBottom"), LinkButton)
            Dim InsertCancelButtonTop As HyperLink = CType(TemplateFormView.FindControl("InsertCancelButtonTop"), HyperLink)
            Dim InsertCancelButtonBottom As HyperLink = CType(TemplateFormView.FindControl("InsertCancelButtonBottom"), HyperLink)
            Dim InsertCancelButtonTop As HyperLink = CType(TemplateFormView.FindControl("InsertCancelButtonTop"), HyperLink)
            Dim InsertCancelButtonBottom As HyperLink = CType(TemplateFormView.FindControl("InsertCancelButtonBottom"), HyperLink)

Open in new window

0
 

Author Closing Comment

by:AusBoyz
ID: 31598873
The CType() was it!

Thank you very much for your help davrob!

0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question