Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-29
4
Medium Priority
?
459 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

598 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