RESOLVED: How to call a public method from Page Prerender event of master page?
Hi Everybody,
I am using ASP.NET 3.5. In my ASP.NET project, I have created a master page, which I have used in all my web forms. I also have code modules in which I have public procedures and functions.
I have written some code in the PreRender page event in the master page, so that it is executed on all my web forms that are web content forms of the master page. However, in that event I am not able to call public functions that I have declared in my code modules, because if I do so, I get a run-time error which says that the function is not declared.
Is it possible to do that. If so how?
Re: How to call a public method from Page Prerender event of master page?
Hey,
Is it possible that you can show come of the code that you are trying to use?
Gary
Re: How to call a public method from Page Prerender event of master page?
Yeah, definitely. Following is the code that I have used in the master page:
Code:
Imports System.IO, System.Xml, System.Xml.XPath, System.Text, System.Data, System.Data.SqlClient
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
If Not Page.IsPostBack AndAlso Session("vEMSMenuOption") <> Nothing Then
' Allow user to access the web page only if he has the right to access it.
Dim vUser_Rights_Master As SqlDataReader, _
vSQL As String = "SELECT [option]" & _
" FROM User_Rights_Master" & _
" WHERE user_name = '" & Replace(CStr(Session("vuser_name")), "'", "''") & _
"' AND [option] = '" & Replace(CStr(Session("vEMSMenuOption")), "'", "''") & "'"
vUser_Rights_Master = GetSQLDataReader(vSQL, fConnectionString(CStr(Application.Get("vCommonDatabaseName"))))
If Not vUser_Rights_Master.Read Then
Response.Redirect("Default.aspx?verror_message=Not allowed to access this module.")
End If
End If
End Sub
End Class
GetSQLDataReader & FConnectionString are public functions that I have declared in code modules, whose code is as follows:
Code:
Public Function GetSQLDataReader(ByVal vSQL As String, Optional ByVal vConnectionString As String = Nothing) As SqlDataReader
' Returns a SQL datareader populated with the specified SQL command.
If vConnectionString = Nothing Then
vConnectionString = Current.Session("vConnectionStringCompany")
End If
Dim vConnection As New SqlConnection(vConnectionString)
Dim vCommand As New SqlCommand(vSQL, vConnection)
vCommand.CommandTimeout = 10000
Dim vSQLDataReader As SqlDataReader
vConnection.Open()
vSQLDataReader = vCommand.ExecuteReader(CommandBehavior.CloseConnection)
Return vSQLDataReader
End Function
Public Function fConnectionString(ByVal vDatabaseName As String) As String
' This function returns a connection string of the specified database
Dim vConnectionString As String, vInitialCatalog As String
vConnectionString = Current.Application.Get("vConnectionString")
vInitialCatalog = vDatabaseName
Return String.Format(vConnectionString, vInitialCatalog)
End Function
Re: How to call a public method from Page Prerender event of master page?
Hey,
Also, is it possible to get the exact error message that you are receiving? Have you got a stack trace?
Gary
Re: How to call a public method from Page Prerender event of master page?
IF your "code module" are classes in your app_code folder then just declare them "public shared function" and you will be able to use them.
Re: How to call a public method from Page Prerender event of master page?
The problem got resolved after transferring all the code modules to the App_Code folder and declaring all the modules as public. The code modules are not classes. They are modules (created by choosing the "Module" option in the New Item dialog box).
Thanks to all!
Re: RESOLVED: How to call a public method from Page Prerender event of master page?
Hey,
Glad to hear that you got it working!
Out of interest, where were the Modules before? You say you moved them into the App_Code folder, but where were they before? For a Web Site Template, all code files should live in the App_Code folder.
Gary
Re: RESOLVED: How to call a public method from Page Prerender event of master page?
They were in the root folder of the project, where the web pages are present.
Re: RESOLVED: How to call a public method from Page Prerender event of master page?
Ah, I see. For a Web Site Template, you should put all your code in the App_Code Folder.
Gary