Solved

Reference Controls from Class

Posted on 2008-10-15
5
273 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
  • 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

Industry Leaders: 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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