How to Find a Control from a Function in App_Code (ASP.NET)
Posted on 2011-09-29
Hello Experts -
I have an aspx page; the code-behind calls a function in the App_Code folder. I'm trying to get the function to set the value of a label in the aspx page.
The aspx page uses a master page, which I know can complicate finding controls.
This is the relevant code in the Public Function (App_Code module):
thePage = HttpContext.Current.Handler
Dim ctlPlace As ContentPlaceHolder = thePage.FindControl("navigationpanel")
Dim ctlLBLhdr As Label = ctlPlace.FindControl("lblMsgHdr")
ctlLBLhdr.Text = strHdr
The "object not set" error appears at the last line.
When stepping through this code, thePage sets correctly, exposing the page that called this function. But the next two variables (ctlPlace and ctlLBLhdr) are set to nothing. I've also tried it without the ContentPlaceHolder, as in thePage.FindControl("lblMsgHdr").
When holding the mouse over thePage variable in debug mode, I can drill down and see the label control, I'm just missing the right approach to set it here.
Any suggestions are most appreciated!