Solved

Reference Controls from Class

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

21 Experts available now in Live!

Get 1:1 Help Now