Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 25th, 2003, 12:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default Question about Subs

I've written a sub that i want to use on more than one page. How can I write it to use multiple times on different page? Here is the code:

Sub BindHomeAudioGrid()
        Dim strConn As String = ConfigurationSettings.AppSettings("cab1esdb")
        Dim objConn As New OleDbConnection(strConn)
        Dim strSQL As String = "SELECT * FROM home_audio ORDER BY id ASC"

        Dim objAdapter As New OleDbDataAdapter(strSQL, objConn)
        Dim objDataSet As New DataSet
        objAdapter.Fill(objDataSet, "HomeAudio")

        Dim r As DataRow

        Dim objTable As New HtmlTable
        Dim objRow As New HtmlTableRow
        Dim objCell As New HtmlTableCell

        objTable.Border = 1
        objTable.Width = "185"
        objTable.CellPadding = 0
        objTable.CellSpacing = 0
        objTable.BorderColor = "#000000"

        objRow.ID = "homeaudio"
        objCell.InnerHtml = "&nbsp;<strong>Home Audio</strong>"
        objCell.Width = 130
        objRow.Cells.Add(objCell)

        objTable.Rows.Add(objRow)

        For Each r In objDataSet.Tables(0).Rows
            objRow = New HtmlTableRow
            objTable.Rows.Add(objRow)

            objCell = New HtmlTableCell
            objRow.ID = "audproducts"
            objCell.InnerHtml = "<a class=category href=category.aspx?loc=1&h=" & r("id") & ">&nbsp;&nbsp;&nbsp;" & r("prod_name") & "</a>"
            objRow.Cells.Add(objCell)
            objTable.Rows.Add(objRow)

            objTable.Rows.Add(objRow)
        Next
        display1.Controls.Add(objTable)
    End Sub

display1 is: Protected WithEvents display1 As System.Web.UI.HtmlControls.HtmlGenericControl
  #2 (permalink)  
Old November 25th, 2003, 12:44 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Probably your best bet is to modify your sub to accept "display1" as an argument and set the sub to be shared so it's visible from outside of an instance of the page. Do everything you are doing as-is, but change the last line to reference the argument. Also you should make sure to make the argument ByRef instead of the default ByVal.

Public Shared Sub BindHomeAudioGrid(ByRef objHtmlControl As HtmlGenericControl)
    ...
    objHtmlControl.Controls.Add(objTable)
End Sub

An even better idea would be to do this as a function that just returns the html table.

Public Shared Function BindHomeAudioGrid() As HtmlTable
    ...
    Return objTable
End Sub

This would be a better technique I think. Then you can call this function from wherever you need...

somePageControl.Controls.Add(otherPageClass.BindHo meAudioGrid())

And you might want to then change the function to something more applicable, like BuildHomeAudioTable().

Peter
------------------------------------------------------
Work smarter, not harder.
  #3 (permalink)  
Old November 25th, 2003, 02:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thanks for your help, Peter.

I tried both of them... but I couldn't figure out how to call the Public Shared Sub on my page.

  #4 (permalink)  
Old November 25th, 2003, 02:30 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If this is the page class with the sub on it (in myFirstPage.aspx.vb)...

Public Class myFirstPage : Inherits System.Web.UI.Page
    ...
    Public Shared Function BindHomeAudioGrid() As HtmlTable
        ...
        Return objTable
    End Sub
End Class


Then in your other page class (mySecondPage.aspx.vb) you call it like this:

myFirstPage.BindHomeAudioGrid()


Of course, this will only work if you are using code-behind. If you are doing this all with in-line code in the ASPX you are out of luck. You need to build this kind of stuff using codebehind so all the classes are compiled together. I don't know if there is any kind of workaround for the scenario of inline code.

Peter
------------------------------------------------------
Work smarter, not harder.
  #5 (permalink)  
Old November 25th, 2003, 04:09 PM
Authorized User
 
Join Date: Jul 2003
Location: Calgary, Alberta, Canada.
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter is right. This will only work with Code Behind as it is all compiled to the CLR. Inline code in not compiled and as a result is not aware of any other classes or methods located elsewhere in yur app.

  #6 (permalink)  
Old November 25th, 2003, 06:10 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by CraigJones
Inline code in not compiled and as a result is not aware of any other classes or methods located elsewhere in yur app.
This is not entirely true. When the framework loads an ASPX, it actually generates a temp vb file with a class named like this: "<pagename>_aspx". It then compiles that to a temp DLL. The class from the DDL the resides in the CLR for future use.

If you are writing ASPX with inline code, and you get an error, you should see two links:

- Show Detailed Compiler Output:
- Show Complete Compilation Source:

The first will show you the call to the compiler to build the temp VB file, and the second is actually the complete temp VB source. It's pretty neat to look through and see all the things it does.

Peter
------------------------------------------------------
Work smarter, not harder.
  #7 (permalink)  
Old November 25th, 2003, 06:40 PM
Authorized User
 
Join Date: Jul 2003
Location: Calgary, Alberta, Canada.
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is that where to JIT compliation comes into play? I have heard about JIT but dont really know to much about it.

  #8 (permalink)  
Old November 26th, 2003, 08:53 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Precisely. Here's the way it happens (basically):

Inline code method
Source(s):
    - ASPX with objects
    - inline code (in same ASPX)

Design-time:
    Steps:
        - Write the ASPX file
Run-time:
    Steps:
        - Create temp vb file from inline code
        - Compile temp vb file to temp DLL containing generic .net MSIL code
        - JIT compile the ASPX into a temporary class (<pagename>_aspx) into CLR
        - JIT compile the MSIL code (from DLL) into platform specific machine code into the CLR
        - Run the code from the CLR

Code-behind method
Source(s):
    - ASPX with objects
    - Code-behind in *.vb files

Design-time:
    Steps:
        - Write the files (*.AS?X, *.vb)
        - Compiled all *.vb into MSIL DLL
Run-time:
    Steps:
        - JIT compile the ASPX into a temporary class (<pagename>_aspx) into CLR
        - JIT compile the MSIL code (from DLL) into platform specific machine code into the CLR
        - Run the code from the CLR

MSIL - MicroSoft Intermediate Language (the .Net generic code)
CLR - Common Language Runtime
JIT - Just In Time (basically, on request compiling)

I'm not sure what goes on with the ASPX. Whether it also gets compiled into a temp MSIL DLL, then JIT-ed into the CLR. I assume that's the case. Using either method, a temp class is created from the ASPX file, where it lives in the mystery of the .net black box.

Peter
------------------------------------------------------
Work smarter, not harder.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preview Tabs not displaying Main or OnDemand Subs ckwizard77 ASP.NET 2.0 Professional 0 September 12th, 2007 01:01 PM
Subs in module not accessible!? LenexaKS ASP.NET 2.0 Basics 4 May 31st, 2007 08:06 AM
Problem passing variables between Subs dgr7 Beginning VB 6 2 February 16th, 2007 06:40 PM
Calling external classes and/or subs/funcs jmischo VBScript 2 December 16th, 2004 08:45 AM
Calling subs & Functions Zooker Beginning VB 6 4 August 20th, 2003 07:09 PM



All times are GMT -4. The time now is 11:58 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.