Include file for code behind vb page for .aspx page

G

Guest

I have some code repeated in many page_load methods in an ASP.NET app.
I have buttons that are enabled or disbaled depending upon the state of a
variable in the session.
Is there a way to put that code in a file and just include it in all of the
files;
...or is there a better way?
Thx
--
David Jones
Senior Lecturer
School of Electrical & Computer Engineering
RMIT University
+61 3 99255318
 
G

Guest

Thanks for the reply.
My problem is that I want to include code in the .vb page (code behiind the
..aspx page), not the .aspx page itself.

Thx
--
David Jones
Senior Lecturer
School of Electrical & Computer Engineering
RMIT University
+61 3 99255318
 
B

Brock Allen

Umm, I'm not sure what you mean by "include the code" in your code behind.
..NET doesn't support anything like C/C++'s #include. Code resuse is at the
binary level, so what you'd have to do is factor out the common code into
either a base class or simply make the common code accessible via static
methods in some other class. The from your codebehind just call those static
methods. For a silly example:

class MyHelper
{
public static void MakeAllControlsVisible(Page p)
{
foreach (Control c in p.Controls)
{
c.Visible = true;
}
}
}

Then in your codebehind:

class MyPage : Page
{
void Page_Load(...)
{
if (!IsPostBack) MyHelper.MakeAllControlsVisible(this);
}
}


-Brock
DevelopMentor
http://staff.develop.com/ballen
 
G

Guest

Here is my solution.
I had to drill into the second control returned from the iteration suggested
by Brock.
There were 3 controls in Brock's interation, the second being the form.

Interating over the controls in the form control found the command buttons I
wanted:
=========
Public Class pageHelper

Sub MakeCommandControlsVisible(ByVal user As String, ByVal p As Page)
Dim c As Control
Dim cd As New System.Web.UI.WebControls.Button
Dim fm As New System.Web.UI.HtmlControls.HtmlForm
For Each c In p.Controls
' I found at this level that 3 controls were in the collection.
' The second control was the form on the page
' So drill into this to get teh form controls
If Object.ReferenceEquals(c.GetType, fm.GetType) Then
Dim cntrl As Control
For Each cntrl In c.Controls
If Object.ReferenceEquals(cntrl.GetType, cd.GetType) Then
' If the form control is a command button then copy
it to
' a command button variable
Dim commd As System.Web.UI.WebControls.Button
commd = cntrl
' Depending upon which user set correct combination
of visble buttons
Select Case user
Case "Anon"
Select Case commd.Text
Case "Login"
commd.Enabled = True
Case "Browse"
commd.Enabled = True
Case "new Blog"
commd.Enabled = False
Case "Select"
commd.Enabled = True
Case "View Images"
commd.Enabled = False
End Select
Case "User"
Select Case commd.Text
Case "Login"
commd.Enabled = False
Case "Browse"
commd.Enabled = True
Case "new log"
commd.Enabled = False
Case "Select"
commd.Enabled = True
Case "View Images"
commd.Enabled = True
End Select
Case "Author"
Select Case commd.Text
Case "Login"
commd.Enabled = False
Case "Browse"
commd.Enabled = True
Case "new Blog"
commd.Enabled = True
Case "Select"
commd.Enabled = True
Case "View mages"
commd.Enabled = True
End Select
Case Else
End Select
End If
Next
End If

Next
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top