Saturday, March 24, 2012

panels and variable state

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>
</htmlHTTP 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.

"Lerp" <admin@.officience.ca> wrote in message
news:OqL6aLPSEHA.3452@.TK2MSFTNGP10.phx.gbl...
> 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>
ah man, I knew it...

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

Thx Kevin,

Cheers, Lerp

"Kevin Spencer" <kspencer@.takempis.com> wrote in message
news:OlXcGbYSEHA.3768@.TK2MSFTNGP11.phx.gbl...
> 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.
> "Lerp" <admin@.officience.ca> wrote in message
> news:OqL6aLPSEHA.3452@.TK2MSFTNGP10.phx.gbl...
> > 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>

0 comments:

Post a Comment