Solved

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

Posted on 2011-09-29
4
426 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now