Solved

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

Posted on 2011-09-29
4
436 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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