Results 1 to 10 of 10

Thread: [RESOLVED] Accessing Web Form controls from Module code?

Hybrid View

  1. #1
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    Resolved [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,
    Blake

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 04
    Location
    Inside the CPU...
    Posts
    6,483

    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.

     
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    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,
    Blake

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 04
    Location
    Inside the CPU...
    Posts
    6,483

    Re: Accessing Web Form controls from Module code?

    vb.net Code:
    1. Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
    2.     Dim ex As Exception = Server.GetLastError
    3.     Label1.Text = "Error: " & ex.Message
    4. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    Re: Accessing Web Form controls from Module code?

    That makes sense!

    Thanks Pradeep
    Blake

  6. #6
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,735

    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

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 04
    Location
    Inside the CPU...
    Posts
    6,483

    Re: Accessing Web Form controls from Module code?

    Quote Originally Posted by gep13 View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    Re: Accessing Web Form controls from Module code?

    Thanks guys!
    Blake

  9. #9
    PowerPoster
    Join Date
    Jan 04
    Posts
    3,616

    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,
    Blake

  10. #10
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,735

    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
  •