1

Prior to upgrading to .NET 4.5 the code was working correctly with findControl, but it was because they were being added with the prepended placeholder location. We are now receiving

Object reference not set to the instance of an object. 

on code that looks like this;

Dim toDate As String = NullConvert.ToString(CType(Page.FindControl("ctl00$ContentPlaceHolder1$Transactions1$txtToDate"), TextBox).Text)

this was working perfectly in the past, but is no longer working. I have tried eliminating the pre-pended contentplaceholder reference to just;

Dim toDate As String = NullConvert.ToString(CType(Page.FindControl("txtToDate"), TextBox).Text)

but that receives the same error. I believe the issue is because the codebehind is in an included HeaderControl Master page, not in the page that is actually calling the _click event of the button. Do I need to reference the child page to find this or is there a better solution? I am not the most advanced .NET programmer if you can't tell. Thank you

Nick G
  • 1,209
  • 8
  • 34
  • 58
  • Where is this textbox on the page? You should use the naming-container control which is on your page and is directly accessible from there as root for `FindControl`. What is `Transactions1`? – Tim Schmelter Oct 20 '14 at 16:08
  • it's just included within a
    with no ID, i can edit the div and give it an ID but I did try that and it didn't work for me before since the Master page was then looking for the div ID within itself and it wasn't there
    – Nick G Oct 20 '14 at 16:09
  • Again, what is `Transactions1`? – Tim Schmelter Oct 20 '14 at 16:11
  • my mistake, that's the ID of the container, the page i was viewing is an .ascx page included in the .aspx page, I will try using the container control to reference. – Nick G Oct 20 '14 at 16:44
  • That doesn't seem to do the trick, is the syntax just FindControl("Transactions1$txtToDate")? or am i referencing it incorrectly? sorry for the lamen – Nick G Oct 20 '14 at 18:26
  • No. How is the UserControl added to the page? Normally you should be able to access it directly. Transactions1.FindControl(...). But you should expose a public property in the control inatead which gets/sets the TextBox.Text and has a meaningful name like ToDate. – Tim Schmelter Oct 20 '14 at 19:01
  • It's a static control just to display a datepicker and the code grabs the selected date, it defaults to today's date; – Nick G Oct 20 '14 at 19:05
  • i also did try using the Transactions1.FindControl and that didn't work, i've tried declaring the ContentPlaceHolder and searching off of that as well as just using Page.FindControl("txtToDate") but that didn't work either – Nick G Oct 20 '14 at 19:06
  • You should read: http://www.codeproject.com/Articles/8797/Mastering-Page-UserControl-Communication Provide public properties in the UC to communicate between the page and the UserControl. You should not expose the whole `TextBox` but only it's `Text` property. Since you have access to the reference of `Transactions1` you can then use that property as: `Me.Transactions1.ToDate = "value"` (if it's a real `Date` i'd also use a `Date`-property). – Tim Schmelter Oct 20 '14 at 19:46

0 Answers0