Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move a function from aspx page to module? 3

Status
Not open for further replies.

jgoodman00

Programmer
Jan 23, 2001
1,510
I am using a dropdown within a datagrid to edit existing records in an underlying table.

I used the example from 4guysfromrolla.com & modified the GetSelIndex function slightly for my needs. I currently have it on my aspx page.
Code:
    Function GetSelIndex(ByRef TableName As String, ByRef ColumnName As String, ByRef Val As String) As Integer
        Dim iLoop As Integer
        'Loop through each row in the DataSet
        Dim dt As DataTable = Me.dsMisc.Tables(TableName)
        For iLoop = 0 To dt.Rows.Count - 1
            If Int32.Parse(Val) = _
                  Int32.Parse(dt.Rows(iLoop)(ColumnName)) Then
                Return iLoop
            End If
        Next iLoop
    End Function

I now have several aspx pages, & need to use this function on all of them, so I want to move the function into a module so all of the pages have access to the same function.

The only problem with this is the dataset. The dataset is on each aspx page, but I need to pass this into the function. I have tried doing this from the aspx page:
Code:
SelectedIndex='<%# GetSelIndex("sp_StaffName", "StaffID", Container.DataItem("StaffID"),dsMisc) %>'
Or
Code:
SelectedIndex='<%# GetSelIndex("sp_StaffName", "StaffID", Container.DataItem("StaffID"),"dsMisc") %>'
Neither of these work though.

Any suggestions?


James Goodman MCSE, MCDBA
 
You should use the idea of BasePage that the other ASPX pages inherite from so that the function is available to all
Add a .vb to your project and add this code.

'Here is your base page.

Public Class BasePage
Inherits System.Web.UI.Page


Public Function GetSelIndex(ByRef TableName As String, ByRef ColumnName As String, ByRef Val As String) As Integer
Dim iLoop As Integer
'Loop through each row in the DataSet
Dim dt As DataTable = Me.dsMisc.Tables(TableName)
For iLoop = 0 To dt.Rows.Count - 1
If Int32.Parse(Val) = _
Int32.Parse(dt.Rows(iLoop)(ColumnName)) Then
Return iLoop
End If
Next iLoop
End Function

End Class

Then on the other pages change "Inherits System.Web.UI.Page" to "Inherits BasePage"




 
I have found a problem:
The line
Code:
Dim dt As DataTable = Me.dsMisc.Tables(TableName)
fails because the base class does not have such a dataset as they are declared on the actual aspx pages.

I tried creating my dataset object on the base class, but the inherited pages do not seem able to use it...

Any suggestions?


James Goodman MCSE, MCDBA
 
You should be able to do that I think that if you put the DataSet declaration on the BasePage this would work

Public Class BasePage
Inherits System.Web.UI.Page

'DataSet accessible for BasePage and Class that inherites from basepage.
Protected dsMisc As DataSet

...
'Functions
...

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top