Solved

Reference Controls from Class

Posted on 2008-10-15
5
278 Views
Last Modified: 2012-05-05
hi

I have some code below which I call in my procedure to handle errors. I want to put this into class so that I don't have to keep on pasting this code into every page. Problem is that I cannot reference controls or use "Request" I use the request.servervariables so that i can view detailed error msgs but only from my ip address

Any ideas on how to do this
Private Sub ErrorHandler(ByVal ex As Exception, ByVal strMethod As String)
		pnlError.Visible = True
		pnlGUI.Visible = False
		lblErrorMSG.Text = "<p>An error has occured please try again "
		If Request.ServerVariables("REMOTE_ADDR") = "IP ADRESS" Then
			lblErrorMSG.Text += "Error Description: Problem in """ & strMethod & """:<br /> " & ex.Message
			txtError.Text = txtError.Text & vbCrLf & "StackTrace:" & vbCrLf & ex.StackTrace
			txtError.Text = txtError.Text & vbCrLf & "Method:" & ex.TargetSite.ToString()
			txtError.Text = txtError.Text & vbCrLf & "URL: " & Request.RawUrl
		Else
			txtError.Visible = False
		End If
	End Sub

Open in new window

0
Comment
Question by:patelajk
[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
  • 3
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 125 total points
ID: 22719278
Hello patelajk,

The second one is the easy part,

HttpContext.Current.Request.ServerVariables()

Will allow you to use that object, in fact any of the objects: server/response/request etc can all be dealt with the same way.

As for the first one, you could pass in a reference to the page object and handle it that way:

Public Sub ErrorHandler(ByRef CallingPage As System.Web.UI.Page, ByVal ex As Exception, ByVal strMethod As String)

Then use CallingPge.pnlError.Visible etc

Called by passing in the relevant object:

MyErrorHandlingClass.ErrorHandler(Me,ex,"Some Method")

Regards,

TimCottee
0
 

Author Comment

by:patelajk
ID: 22719446
>> ByRef CallingPage As System.Web.UI.Page

would i pass the .aspx page here
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 22719475
patelajk,

From the page where you would call it, pass the page in as "Me"  as here:
MyErrorHandlingClass.ErrorHandler(Me,ex,"Some Method")

TimCottee
0
 

Author Comment

by:patelajk
ID: 22719543
Thanks for that... but got another problem now

I got this in my class
Public Class ErrorCatching

      Public Shared Sub ErrorHandler(ByRef CallingPage As System.Web.UI.Page, ByVal ex As Exception, ByVal strMethod As String)
            Dim pnlError As Panel = CallingPage.FindControl("pnlError")
            Dim pnlGUI As Panel = CallingPage.FindControl("pnlGUI")
            Dim lblErrorMSG As Label = CallingPage.FindControl("lblErrorMSG")
            Dim txtError As TextBox = CallingPage.FindControl("txtError")

            pnlError.Visible = True
            pnlGUI.Visible = False
end sub

and in my .aspx page
i have
ErrorCatching.ErrorHandler(Me, ex, "GetClientName")

i get an error saying that it can't find the controls i.e. pnlError which is on the .aspx page
0
 

Author Comment

by:patelajk
ID: 22719550
tim
it seems to fail @ pnlError.Visible = True -- in the class
0

Featured Post

Independent Software Vendors: 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

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…
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.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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