|
-
Feb 16th, 2011, 03:14 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Accessing Web Form controls from Module code?
I know in standard VB.Net development, you can access any form field from within a module class but I'm not sure how to do it using Web Forms.
Here is my scenario:
I have a Module.vb where I am putting all my Database Functionality. In each of my functions, I'm using a Try Catch construct. Is is possible to display an error from within my Module.vb on any web form? If so, how is it done?
Thanks,
-
Feb 16th, 2011, 03:21 PM
#2
Re: Accessing Web Form controls from Module code?
While you can't do a Response.Redirect etc. from within a module, the easiest way to handle such situations is to throw the exception. Then the form that called the method in the Module can handle the error and appropriately redirect to the error page.
I usually prefer not to put any Try..Catch blocks in the Module methods, and let it error out. This way, the calling form can handle the situations in its own way.
e.g. In the Page_Error event, redirect to your error page etc.
 
-
Feb 16th, 2011, 03:28 PM
#3
Thread Starter
PowerPoster
Re: Accessing Web Form controls from Module code?
Ok,
On our web forms we use an asp Label control at the top of the web page to display any messages.
So, on a form that has this label, how do I populate the label with an error message from the Page_Error Event?
Thanks,
-
Feb 16th, 2011, 03:56 PM
#4
Re: Accessing Web Form controls from Module code?
vb.net Code:
Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error Dim ex As Exception = Server.GetLastError Label1.Text = "Error: " & ex.Message End Sub
-
Feb 16th, 2011, 06:13 PM
#5
Thread Starter
PowerPoster
Re: Accessing Web Form controls from Module code?
That makes sense!
Thanks Pradeep
-
Feb 18th, 2011, 02:47 AM
#6
Re: Accessing Web Form controls from Module code?
Equally, this could be handled in the page, where you are actually calling into the module, rather than bubbling it up to the page level.
Gary
-
Feb 18th, 2011, 04:28 AM
#7
Re: Accessing Web Form controls from Module code?
 Originally Posted by gep13
Equally, this could be handled in the page, where you are actually calling into the module, rather than bubbling it up to the page level.
Gary
Yes exactly!
Ideally you should try to handle the error from where it is called.
Only unexpected errors or common error handling routine should go in the Page Error event.
-
Feb 18th, 2011, 09:56 AM
#8
Thread Starter
PowerPoster
Re: Accessing Web Form controls from Module code?
-
Jul 17th, 2012, 09:38 PM
#9
Thread Starter
PowerPoster
Re: [RESOLVED] Accessing Web Form controls from Module code?
Ok guys,
I have a WebUserControl. In the code-behind, I have several functions that contain a Try Catch construct. In the Catch, I wish to capture any errors. I have a Label Control on the main page that shows all application errors. I want to be able to display any errors from the UC in this Label as well. Below is what my code looks like in the UC within the Catch phrase.
Code:
Private Sub DisplayCatalog(ByVal intCatalogID As Integer)
Try
divProductType.Visible = False
divResulstsPerPage.Visible = True
divSC.Visible = True
LoadCatalogItems(intCatalogID)
Catch ex As Exception
strErrMsg = "UCCatalog/DisplayCatalog() - " & ex.Message
End Try
End Sub
Is there a way to define a "Global" variable or something?
Thanks,
-
Jul 18th, 2012, 12:47 AM
#10
Re: [RESOLVED] Accessing Web Form controls from Module code?
Hello,
There are a couple of ways that you can do this...
Probably the cleanest way would be to bubble an event from the User Control, to the containing page to indicate that "something" went wrong. Within this event bubble, you can provide information about what exactly went wrong.
An article showing how this can be achieved can be found here:
http://odetocode.com/code/94.aspx
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|