?
Solved

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

Posted on 2011-09-29
4
Medium Priority
?
443 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
[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
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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