|
-
Oct 26th, 2009, 04:21 AM
#1
Thread Starter
Hyperactive Member
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?
Last edited by Utpal; Oct 27th, 2009 at 01:50 AM.
It is easy when you know it.
-
Oct 26th, 2009, 04:33 AM
#2
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
-
Oct 26th, 2009, 07:41 AM
#3
Thread Starter
Hyperactive Member
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
It is easy when you know it.
-
Oct 26th, 2009, 10:00 AM
#4
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
-
Oct 26th, 2009, 07:10 PM
#5
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.
-
Oct 27th, 2009, 01:48 AM
#6
Thread Starter
Hyperactive Member
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!
It is easy when you know it.
-
Oct 27th, 2009, 02:07 AM
#7
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
-
Oct 28th, 2009, 03:06 AM
#8
Thread Starter
Hyperactive Member
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.
It is easy when you know it.
-
Oct 28th, 2009, 03:19 AM
#9
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
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
|