Solved

How to Find a Control from a Function in App_Code (ASP.NET)

Posted on 2011-09-29
4
427 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:JMS1965
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36869194
Convert your handler to Page class.

Below template could help you.

Page page = HttpContext.Current.Handler as Page;

if (page != null)
 {  
   // Your FindControl code comes here.
}
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 36880481
If you want to access on textbox or any other control in app_cpde you can pass that control as reference to that function as below.

' This is your Page_Load event located at your code behind file.

Protected Sub Page_Load(sender As Object, e As EventArgs)
      PageAccess.UpdateGrid(Label1)
End Sub


' This is the static method located in your .cs file.
Public Shared Sub UpdateGrid(ByRef Label1 As Label)
      Label1.text = ""
End Sub
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36881020
Small change when calling

' This is your Page_Load event located at your code behind file.

Protected Sub Page_Load(sender As Object, e As EventArgs)
      PageAccess.UpdateGrid(ByRef Label1)
End Sub


0
 

Author Closing Comment

by:JMS1965
ID: 36892303
Thanks very much!  

I actually ended up passing (byRef) a panel from the calling page. This allowed the function in App_Code file to work with any control within the panel.

Have a great day!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable and re-enable a dynamic System.Timers.Timer 6 40
ASP.NET reading ATOM 2 52
Auto refresh part of an MVC5 View 7 60
Host asp.net pages 5 25
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now