Solved

Reference Controls from Class

Posted on 2008-10-15
5
242 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
Comment Utility
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
Comment Utility
>> ByRef CallingPage As System.Web.UI.Page

would i pass the .aspx page here
0
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
Comment Utility
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
Comment Utility
tim
it seems to fail @ pnlError.Visible = True -- in the class
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBSCRIPT in Page Load C# ASP.NET 6 51
WPF issue with Trigger 2 65
crm development 2 34
ASP.NET reading ATOM 2 20
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

9 Experts available now in Live!

Get 1:1 Help Now