boolean problem and viewstate

  • Thread starter Thread starter mark
  • Start date Start date
M

mark

i have a form with buttons - when a button is pressed a boolean is set to
true
to show that the record has been edited
i have lots of buttons and i seem to lose the value on the boolean on page
reload
im using code behind in VB with asp

eg

two example buttons

Private Sub factfindcomp_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles factfindcomp.Click
factfindcomplabel.Text = Date.Today
factfindcomplabel.Visible = True
factfindcomp.Visible = False
LiveEdited = True
End Sub
Private Sub complia_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles complia.Click
compltext.Text = Date.Today
compltext.Visible = True
complia.Visible = False
LiveEdited = True
End Sub

when i click another button called save

it checks the state of liveedited = if its true it saves - only problem is
var liveedited seems to be not keeping its value between button presses

eg

If LiveEdited = True Then
Dim a As String = DBAccess.UpdateLive(mainid, msl, pit, prt, smo, ma, dtma,
ms, lend, lr, sol, off, "", com, comp, poscomnot, "", "", "", tsl, cc, "",
"", mr, "", outc, offchk, "", ffc, subtyp)
If a = "ERROR - Function Is Broke" Then
RegisterStartupScript("startupScript", "<script
language=JavaScript>alert('ERROR 102');</script>")
End If
LiveEdited = False

only seems to be doing it on button clicks

how can i assure save state on a boolean ?

cheers mark

ive tried viewstate but i cant seem to get it to work!
 
Check to see if you are doing a IsPostBack check. Many times when things
start disapeering like that it is because you are wiping the variable out on
postback.

-Stanley
 
Stanley said:
Check to see if you are doing a IsPostBack check. Many times when things
start disapeering like that it is because you are wiping the variable out on
postback.

-Stanley
how do i check for that ?

thanks

mark
 
if not IsPostBack then
'this is not a postback so we can assume this is the first visit to the
page
else
'this *IS* a postback
end if



-Stanley
 
Stanley said:
if not IsPostBack then
'this is not a postback so we can assume this is the first visit to the
page
else
'this *IS* a postback
end if
so i would do that on page load ?
or on each button click ?

thanks

mark
 
Page_Load. Basically if it is *NOT* a postback then you can initiate some
variables and do some things that only need to happen the first time the
page loads. But in the event you are setting up a default value for your
boolean value in page_load then every time your page loads it is reverting
back to that default. Take a look at the code below. The only time I want to
bind my data on this page is the first time it comes to the page. So I do a
check and bind if it is not a postback.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim dt As New DataTable
Dim row As DataRow
dt.Columns.Add("name")

row = dt.NewRow
row(0) = "Fred Flintstone"
dt.Rows.Add(row)
row = dt.NewRow
row(0) = "Barney Rubble"
dt.Rows.Add(row)
row = dt.NewRow
row(0) = "Wilma Flintstone"
dt.Rows.Add(row)
row = dt.NewRow
row(0) = "Betty Rubble"
dt.Rows.Add(row)

DataGrid1.DataSource = dt
DataGrid1.DataBind()
End If
End Sub
 
The problem isn't "a boolean" per se, but how ASP.Net works. HTTP is
stateless, which means that every request for a Page happens "in a vacuum"
so to speak. There is no persistent connection between browser and server.
The server responds to each request and then "forgets" about it. So, the
real issue is persisting ANY kind of data between PostBacks. Now, some of
your Controls seem to survivie well between PostBacks. That is because they
are part of the class definition, and are therefore rebuilt each time the
class is rebuilt (with each Request). However, any values you set in a Page
dynamically need to be persisted somehow. In this case, it sounds like you
need to add your boolean value to the Page's ViewState. ViewState creates a
hidden form field in the clinet browser page, with various values that need
to be persisted across PostBacks, such as the state of various Controls. You
can add any serializable value to ViewState, including boolean. When the
Page posts back to the server, the contents of ALL form fields are posted
(it IS a form), including the hidden ViewState field. This emulates
persistence by having the data passed back and forth between client and
server.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
thanks for the help but im still struggling with it - im only losing state
because of the buttons - its weird eg

editing a text box works fine :-

Private Sub telhome1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles telhome1.TextChanged
PersonalEdited = True
End Sub
this will keep its state - odd
 
Back
Top