Property values disappear on postback

  • Thread starter Thread starter stephen
  • Start date Start date
S

stephen

Hi,

I make use of properties to store values that are relevant to the page but
this time I am posting the page to itself and the values are Zero or null
(they disappear). is there any other way I can store them/preserve them so
that when I post back I can use the values.
I tried session and it works but some other ideas would be helpful.

Appreciate your help,
Stephen
 
Can you be more specific? What properties of what objects are you setting?
How are you setting them? When are you setting them?
 
Hi Scott,

Thanks for the reply, This is my task. I take an itemnumber, saledate
retieve all info (there can be multiple values) and display it on a grid.
then the user can click on a specific value and now I take the values again
(this time 3 values... itemnumber, saledate and OpType) and retieve info and
display on another grid.

So the first time a user inputs itemnumber and saledate.. and clicks the
button I store it in properties like this
Public Property SPItemNumber() As integer
Get
Return viewstate("SPItemNumber")

End Get
Set(ByVal Value As integer)
ViewState("SPItemNumber") = Value

End Set
End Property

Public Property SPSaleDate() As Date
Get
Return viewstate("SPSaleDate")

End Get
Set(ByVal Value As Date)
ViewState("SPSaleDate") = Value

End Set
End Property


I set it when i user clicks the button to get the information
(firstDatagrid) and this has a itemtemplate like this
<asp:TemplateColumn HeaderText="Op Type" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink runat="server" id="OPLink" text='<%#
DataBinder.Eval(Container.DataItem, "optype") %>' NavigateUrl='<%#
"default.aspx?optype=" & Container.DataItem("optype") %>' />
</ItemTemplate>
</asp:TemplateColumn>

but it clears out when I click on the link.... i know it posts to the same
page... but why does the property value cleared?

Thanks,
Stephen
 
Well what is this property a property of? And, I also don't see any lines
of code that actually call the set portion of your properties.

When are you writing this code:

SPItemNumber = something
SPSaleDate = something
 
Hi Scott,

I am sorry.. i do it at the click event of a button so that it sets the
values that were in the respective textboxes
e.g
SPItemNumber = integer.parse(textbox1.text)
SPSaleDate = date.parse(textbox2.text)
I set the values first then call the stp for the first Datagrid and then
bind it.... then on clicking the "HyperLink" in the datagrid postback to the
same page, call another STP (3 parameters) and bind the second datagrid

Am i presenting the situation correctly this time,
Thanks,
Stephen
 
Not yet. When you have questions of this kind, it's best to provide us with
your code (not just bits and pieces). Let's see all your code.

I think the problem is you are trying to set the property values once they
are already gone.
 
Hi Scott,

Sorry for the late late reply...
here is the code that I use
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
setValues() --> Setting basic values
SPOpType = Request.QueryString("optype")
If Not SPOpType = "" Then
getDetails()
makeVisible() --> Making Panels Visible

Else
makeInvisible() --> Making Panels InVisible

End If

End If
End Sub

Private Sub btnGrid_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGrid.Click
Dim dsCompare As DataSet
SPItemNumber = integer.parse(txtEVID.Text)
SPSaleDate = date.parse(txtSDate.Text)
Try
dsCompare = db.GetCompare(SPItemNumber, SPSaleDate)
If Not dsCompare Is Nothing Then
SPDGCompare = dsCompare
dgCompare.DataSource = dsCompare
dgCompare.DataBind()
pnlCompare.Visible = True

End If

Catch ex As Exception
errLog.LogIt("Binding Compare Grid Error", ex, "")

End Try

End Sub

Private Sub getDetails()
Dim ds As DataSet
Dim dsSum As DataSet
Try
dgCompare.DataSource = SPDGCompare
dgCompare.DataBind()

Catch ex As Exception
errLog.LogIt("Binding Grid1 Error", ex, "")

End Try

Try
ds = db.GetDetails1(SPItemNumber, SPSaleDate, SAPOpType)
dgScroll.DataSource = ds
dgScroll.DataBind()
Catch ex As Exception
errLog.LogIt("Binding Grid2 Error", ex, "")

End Try
Try
dsSum = db.GetDetails2(SPItemNumber, SPSaleDate, SAPOpType)
dgSummary.DataSource = dsSum
dgSummary.DataBind()

Catch ex As Exception
errLog.LogIt("Binding Grid3 Error", ex, "")

End Try
End Sub

Public Property SPItemNumber() As integer
Get
Return viewstate("SPItemNumber")

End Get
Set(ByVal Value As integer)
ViewState("SPItemNumber") = Value

End Set
End Property

Public Property SPSaleDate() As Date
Get
Return viewstate("SPSaleDate")

End Get
Set(ByVal Value As Date)
ViewState("SPSaleDate") = Value

End Set
End Property

Public Property SPOpType() As String
Get
Return viewstate("SPOpType")

End Get
Set(ByVal Value As String)
ViewState("SPOpType") = Value

End Set
End Property

Public Property SPDGCompare() As DataSet
Get
Return viewstate("SPDGCompare")

End Get
Set(ByVal Value As DataSet)
ViewState("SPDGCompare") = Value

End Set
End Property
 
Well, you are using properties (which will be reset each time the page
loads) and ViewState (which will persist) when all you really need is
ViewState and forget the properties all-together. You are making your code
more difficult than it need be.

In the spots where you want to store data for the next page call, just put
that data directly into ViewState and forget the properties. When you need
the data, just pull it out of ViewState.

How about this:

Private Sub Page_Load( . . .) Handles MyBase.Load
If Not IsPostBack Then
setValues() --> Setting basic values
SPOpType = Request.QueryString("optype")
If Not SPOpType = "" Then
getDetails()
makeVisible() --> Making Panels Visible
Else
makeInvisible() --> Making Panels InVisible
End If
End If
End Sub

Private Sub btnGrid_Click( . . . ) Handles btnGrid.Click
Dim dsCompare As DataSet

'No need to cast thse text values since they will become objects in
ViewState anyway
ViewState.Add("SPItemNumber", txtEVID.Text)
ViewState.Add("SPSaleDate", txtSDate.Text)

Try
' DOES db.GetCompare() ON THE NEXT LINE RETURN A DATASET?
' If not, then dsCompare is not going to be instantiated as a
NEW dataset.
dsCompare = db.GetCompare(CType(txtEVID.Text, Integer),
Ctype(txtSDate.Text, Date))

If Not dsCompare Is Nothing Then
ViewState.Add("SPDGCompare", dsCompare)
dgCompare.DataSource = dsCompare
dgCompare.DataBind()
pnlCompare.Visible = True
End If
Catch ex As Exception
errLog.LogIt("Binding Compare Grid Error", ex, "")
End Try
End Sub

Private Sub getDetails()
Dim ds As DataSet
Dim dsSum As DataSet

Dim SPItemNumber As Integer = CType(ViewState("SPItemNumber",
Integer))
Dim SPSaleDAte As Date = CType(ViewState("SPSaleDate", Date))

Try
dgCompare.DataSource = CType(ViewState("SPDGCompare") , DataSet)
dgCompare.DataBind()
Catch ex As Exception
errLog.LogIt("Binding Grid1 Error", ex, "")
End Try

ViewState.Add("SPItemNumber", txtEVID.Text)
ViewState.Add("SPSaleDate", txtSDate.Text)

Try
ds = db.GetDetails1(SPItemNumber, SPSaleDate, SAPOpType)
dgScroll.DataSource = ds
dgScroll.DataBind()
Catch ex As Exception
errLog.LogIt("Binding Grid2 Error", ex, "")
End Try

Try
dsSum = db.GetDetails2(SPItemNumber, SPSaleDate, SAPOpType)
dgSummary.DataSource = dsSum
dgSummary.DataBind()
Catch ex As Exception
errLog.LogIt("Binding Grid3 Error", ex, "")
End Try
End Sub
 
Back
Top