|
-
Sep 24th, 2008, 02:22 AM
#1
Thread Starter
Junior Member
Object-Oriented Programming
I am currently learning to use ASP.NET 2.0 with Visual Web Developer.
For my previous project, I used ASP 3.0 with OOP approach where all my files are .asp regardless they are ASP page or class file.
In .NET, we can use code behind and Master/Detail design.
I tried to practice OOP approach in ASP.NET but yet find any sample or tutorial for it.
I tried to use a class file (eg. Bank.vb) where it will stored inside App_Code folder.
Bank.vb:
Code:
Public Class Bank
Private strProperty1 As String
Private strProperty2 As String
Public Property Property1() As String
Get
Return strProperty1
End Get
Set(ByVal value As String)
strProperty1 = value
End Set
End Property
Public Property Property2() As String
Get
Return strProperty2
End Get
Set(ByVal value As String)
strProperty2 = value
End Set
End Property
End Class
In MasterPage.master.vb, I have:
Code:
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Private objBank As New Bank
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
objBank.Property1 = DropDown1.SelectedValue
objBank.Property2 = DropDown2.SelectedValue
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If RadioButton1.SelectedIndex = 1 Then
Response.Redirect("BankReport.aspx")
Else
Response.Redirect("ProductReport.aspx")
End If
End Sub
Protected Overrides Sub Finalize()
objBank = Nothing '<-Required?
MyBase.Finalize()
End Sub
End Class
ProductReport.aspx.vb:
Code:
Imports Bank
Partial Class ProductReport
Inherits System.Web.UI.Page
Public oBank As New Bank
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write(oBank.Property1) '<- oBank.Property1 = Nothing
End Sub
Protected Overrides Sub Finalize()
oBank = Nothing '<-Required?
MyBase.Finalize()
End Sub
End Class
My problem is why oBank.Property1 = Nothing ?
It should has a value from DropDown1.SelectedValue when I click btnSubmit.
For example, DropDown1.SelectedValue = "Public Bank LLC"
Then this value will assign to Property1 through objBank.Property1
and share in ProductReport.aspx using oBank.Property1
Can you tell me what's wrong with my code?
Should I initialize all my variables in Bank.vb class using:
Code:
Public Sub New()
strProperty1 = ""
strProperty2 = ""
End Sub
Finally, do I need to dereference the objBank and oBank using Finalize Sub?
-
Sep 24th, 2008, 05:49 AM
#2
Re: Object-Oriented Programming
MasterPage.master.vb is a class file. ProductReport.aspx.vb is another class file. They are not the same class file. Therefore when you instantiate Bank on ProductReport, it won't have any property values unless you give it property values.
Remember that a MasterPage is not a base class to any child page. It's just a master page in terms of the page layout and markup.
If you want to access the properties of the Bank object from a page that uses a MasterPage, you will first need to make the Bank object available as a property of the MasterPage. Then in your 'child' page, take Me.Page.Master, convert it to the type of your master page (in other words, the class name of your Master Page which just happens to be MasterPage) and that property is then available for you to use.
-
Sep 24th, 2008, 08:20 AM
#3
Thread Starter
Junior Member
Re: Object-Oriented Programming
Then in your 'child' page, take Me.Page.Master, convert it to the type of your master page...
I am not very clear with this statement. How to convert?
-
Sep 24th, 2008, 08:58 AM
#4
Re: Object-Oriented Programming
Using CType. Like this
Code:
Dim myMasterPage as MasterPage = CType(Me.Page.Master, MasterPage)
TextBox1.Text = myMasterPage.MyPropertyName
-
Sep 26th, 2008, 11:18 AM
#5
Thread Starter
Junior Member
Re: Object-Oriented Programming
I will try and see. Thanks.
-
Oct 10th, 2008, 11:04 AM
#6
Thread Starter
Junior Member
Re: Object-Oriented Programming
I can use Function and Subroutine from the class but the values of class property are reinitialized every time the page.aspx loaded.
Is there any way to keep the values instead of using Session?
-
Oct 11th, 2008, 03:37 AM
#7
Re: Object-Oriented Programming
You can use Session or Viewstate. Viewstate helps persist data across postbacks but it's never a good idea to put too much data or secure data in viewstate because viewstate actually goes back to the client on each postback.
-
Oct 12th, 2008, 10:59 AM
#8
Thread Starter
Junior Member
Re: Object-Oriented Programming
Thanks for introducing to me on Viewstate.
Does this mean that using Class is not the solution to keep the values?
I am working on reporting so I will need to pass the common parameters like Year, Month and some Level Codes through out the drill-down reports.
-
Oct 12th, 2008, 02:16 PM
#9
Re: Object-Oriented Programming
No no, I didn't mean to say that.
What I mean is if you have a lot of data and/or sensitive data, then you shouldn't use viewstate.
In your case, you seem to have a class that contains just a few properties. Year, Month, Level Codes. Assuming 'level codes' aren't a sensitive piece of information, this means that the XML for the viewstate (when you put something in viewstate, it is serialized) won't be that big, so you should be alright with it.
The same thing happens with the session variable - it gets serialized - but the client is sent a 'sessionid' cookie which helps match the right session variable with the right person. The advantage of the session variable is that the user can't see what the session variable actually contains.
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
|