Solved

Reference Controls from Class

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word Directory is not in the drop down list 4 31
Why is some text in blue in Visual Studio? 6 32
Displaying a web form as a modal popup dialog box. 4 30
IIS redirect 1 65
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

18 Experts available now in Live!

Get 1:1 Help Now