newbie object question

G

Guest

Hi Chris,

I didn't try out the code, but am guessing the issue is this: Each time you click a button, you get a postback. But, you're only building the dataview if not postback, so you're ok the first time in, but on subsequent clicks there's no more dataview behind there. By default, the controls themselves will maintain their values in viewstate, but you're on your own for the dataview and any other private variables--either build it each time (skip the if not ispostback), or persist the dataset in viewstate itself (or in session if you want to use it across multiple pages). And in response to your final question, yes, all objects are recreated from scratch after every postback, which is very important to remember, but easy to forget since asp.net does so much to restore state under the covers (like it'll restore the items you see in a datagrid, but not the datasource that's behind that grid). Does that help at all?

Bill

----- Chris Kennedy wrote: -----

I have created an object which has a method which 'binds' the values from a
dataview to some text fields. The row is determined by a property which set
before the method. When I set the property via a button click I can see the
property value change in the debugger. When I click the button again the
value of the property always returns to its original value. What I am doing
probably isn't right way to appoach the problem but its really just an
exercise in using objects. Is the object being initialised again on the
callback? I have included the code below

Public Customer As New BindCustomer()

Public Class BindCustomer

Private pos As Integer

Public Property current_position() As Integer

Get

Return pos

End Get

Set(ByVal Value As Integer)

pos = Value

End Set

End Property

Public Sub MoveNext(ByVal dv As DataView, ByVal com As TextBox, ByVal con As
TextBox, ByVal add1 As TextBox, ByVal add2 As TextBox, ByVal pos As Integer)

pos = pos + 1

com.Text = dv(pos)("CompanyName").ToString

con.Text = dv(pos)("ContactName").ToString

add1.Text = dv(pos)("Address1").ToString

add2.Text = dv(pos)("Address2").ToString

End Sub

Public Sub MovePrevious(ByVal dv As DataView, ByVal com As TextBox, ByVal
con As TextBox, ByVal add1 As TextBox, ByVal add2 As TextBox, ByVal pos As
Integer)

pos = pos - 1

com.Text = dv(pos)("CompanyName").ToString

con.Text = dv(pos)("ContactName").ToString

add1.Text = dv(pos)("Address1").ToString

add2.Text = dv(pos)("Address2").ToString

End Sub

End Class

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If Not Page.IsPostBack Then

SqlDataAdapter1.Fill(DataSet1)

DataView1 = DataSet1.Tables(0).DefaultView

BindCustomers()

End If

End Sub

Private Sub BindCustomers()

CompanyName.Text = DataView1(0)("CompanyName").ToString

ContactName.Text = DataView1(0)("ContactName").ToString

Address1.Text = DataView1(0)("Address1").ToString

Address2.Text = DataView1(0)("Address2").ToString

Customer.current_position = 0

End Sub

Private Sub SqlConnection1_InfoMessage(ByVal sender As System.Object, ByVal
e As System.Data.SqlClient.SqlInfoMessageEventArgs) Handles
SqlConnection1.InfoMessage

End Sub

Private Sub MoveNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MoveNext.Click

Dim pos As Integer

Customer.current_position = Customer.current_position + 1

'Customer.MoveNext(DataView1, CompanyName, ContactName, Address1, Address2,
pos)

End Sub

Private Sub MovePrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MovePrevious.Click

Dim pos As Integer

Customer.current_position = Customer.current_position - 1

'Customer.MoveNext(DataView1, CompanyName, ContactName, Address1, Address2)

End Sub
 
C

Chris Kennedy

I have created an object which has a method which 'binds' the values from a
dataview to some text fields. The row is determined by a property which set
before the method. When I set the property via a button click I can see the
property value change in the debugger. When I click the button again the
value of the property always returns to its original value. What I am doing
probably isn't right way to appoach the problem but its really just an
exercise in using objects. Is the object being initialised again on the
callback? I have included the code below

Public Customer As New BindCustomer()

Public Class BindCustomer

Private pos As Integer

Public Property current_position() As Integer

Get

Return pos

End Get

Set(ByVal Value As Integer)

pos = Value

End Set

End Property

Public Sub MoveNext(ByVal dv As DataView, ByVal com As TextBox, ByVal con As
TextBox, ByVal add1 As TextBox, ByVal add2 As TextBox, ByVal pos As Integer)

pos = pos + 1

com.Text = dv(pos)("CompanyName").ToString

con.Text = dv(pos)("ContactName").ToString

add1.Text = dv(pos)("Address1").ToString

add2.Text = dv(pos)("Address2").ToString

End Sub

Public Sub MovePrevious(ByVal dv As DataView, ByVal com As TextBox, ByVal
con As TextBox, ByVal add1 As TextBox, ByVal add2 As TextBox, ByVal pos As
Integer)

pos = pos - 1

com.Text = dv(pos)("CompanyName").ToString

con.Text = dv(pos)("ContactName").ToString

add1.Text = dv(pos)("Address1").ToString

add2.Text = dv(pos)("Address2").ToString

End Sub

End Class

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If Not Page.IsPostBack Then

SqlDataAdapter1.Fill(DataSet1)

DataView1 = DataSet1.Tables(0).DefaultView

BindCustomers()

End If

End Sub

Private Sub BindCustomers()

CompanyName.Text = DataView1(0)("CompanyName").ToString

ContactName.Text = DataView1(0)("ContactName").ToString

Address1.Text = DataView1(0)("Address1").ToString

Address2.Text = DataView1(0)("Address2").ToString

Customer.current_position = 0

End Sub

Private Sub SqlConnection1_InfoMessage(ByVal sender As System.Object, ByVal
e As System.Data.SqlClient.SqlInfoMessageEventArgs) Handles
SqlConnection1.InfoMessage

End Sub

Private Sub MoveNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MoveNext.Click

Dim pos As Integer

Customer.current_position = Customer.current_position + 1

'Customer.MoveNext(DataView1, CompanyName, ContactName, Address1, Address2,
pos)

End Sub

Private Sub MovePrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MovePrevious.Click

Dim pos As Integer

Customer.current_position = Customer.current_position - 1

'Customer.MoveNext(DataView1, CompanyName, ContactName, Address1, Address2)

End Sub
 
K

Kevin Spencer

Hi Chris,

Yes, as HTTP is stateless, all of your classes must be re-built from scratch
with every PostBack. Changes that occur in them must be persisted somehow.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

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