panels and variable state

L

Lerp

Hi all,

I was just playing around with some code to hide/show panels and I
discovered that I am losing a variable's value when my sub is called from a
button in paneltwo. Why is this?

Cheers and Thanks, Lerp



Code below:


<script language="vb" runat="server">

Dim myvalue as Long

SUB Page_Load(Sender As Object, E As EventArgs)
panelone.visible = true
paneltwo.visible = false
END SUB

Sub One(Sender As Object, E As EventArgs)
myvalue = 7
panelone.visible = false
paneltwo.visible = true
response.write(myvalue & " IS MY VALUE" & "<BR>")
End Sub


Sub Two(Sender As Object, E As EventArgs)
Dim curVal = myvalue
panelone.visible = true
paneltwo.visible = false
response.write(curVal & " IS CurVal VALUE" & "<BR>")
******************************** This turns out to be ZERO

End Sub

</script>


<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head></head>
<body>

<form runat="server">

<asp:panel ID="panelone" runat="server">
<table width="600" cellpadding="2" cellspacing="0" border="0">
<tr valign="top">
<td align="left" class="bodycopy">
<asp:Button id="submitform1" text="Panel One Button" OnClick="One"
runat="server" CssClass="but" />
</td>
</tr>
</table>
</asp:panel>


<asp:panel ID="paneltwo" runat="server">
<table width="600" cellpadding="2" cellspacing="0" border="0">
<tr valign="top">
<td align="left" class="bodycopy">
<asp:Button id="submitform2" text="Panel Two Button" OnClick="Two"
runat="server" CssClass="but" />
</td>
</tr>
</table>
</asp:panel>


</form>
</body>
</html>
 
K

Kevin Spencer

HTTP is stateless. This means that every time a Page Posts back, everything
is re-initialized. If you change the value of a private member of your class
in one instance, you have to persist it somehow between PostBacks.
ViewState, Session, etc.

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

Lerp

ah man, I knew it....

I ended up using session vars anyways...thx for help.

Thx Kevin,

Cheers, Lerp
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top