How to declare a session variable in VS2019 (HTML/CSS/JS with VB Code behind)
-
I'm not sure if 'session variable' is the right word but all I am trying to do here is declare a variable and increment it every time my VB Button is clicked. Any help would be appreciated. Simple HTML code here followed by a few lines of VB...
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="SIMPLE_VARIABLE_PASS_2.WebForm1" %>
.auto-style1 { position: absolute; top: 79px; left: 185px; z-index: 1; width: 80px; } <!--THIS IS PART OF THE PASSING SETUP name = 6 document.getElementById('<%= Name.ClientID %>').value = name;
--------------------------------
Public Class WebForm1
Inherits System.Web.UI.PageProtected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click Name.Value = Name.Value + 1 Label1.Text = Name.Value End Sub
End Class
-
I'm not sure if 'session variable' is the right word but all I am trying to do here is declare a variable and increment it every time my VB Button is clicked. Any help would be appreciated. Simple HTML code here followed by a few lines of VB...
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="SIMPLE_VARIABLE_PASS_2.WebForm1" %>
.auto-style1 { position: absolute; top: 79px; left: 185px; z-index: 1; width: 80px; } <!--THIS IS PART OF THE PASSING SETUP name = 6 document.getElementById('<%= Name.ClientID %>').value = name;
--------------------------------
Public Class WebForm1
Inherits System.Web.UI.PageProtected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click Name.Value = Name.Value + 1 Label1.Text = Name.Value End Sub
End Class
Member 14226307 wrote:
<script>
<!--THIS IS PART OF THE PASSING SETUP
name = 6
document.getElementById('<%= Name.ClientID %>').value = name;
</script>You're resetting the hidden field every time the page loads. When you click the button, the server-side code runs, increments the value, and returns the HTML and Javascript to the browser. The Javascript then runs, and resets the hidden field back to
6
. You should only initialize the hidden field in thePage_Load
event, inside anIf Not Page.IsPostBack
block:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Name.Value = "6"
Label1.Text = Name.Value
End If
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Member 14226307 wrote:
<script>
<!--THIS IS PART OF THE PASSING SETUP
name = 6
document.getElementById('<%= Name.ClientID %>').value = name;
</script>You're resetting the hidden field every time the page loads. When you click the button, the server-side code runs, increments the value, and returns the HTML and Javascript to the browser. The Javascript then runs, and resets the hidden field back to
6
. You should only initialize the hidden field in thePage_Load
event, inside anIf Not Page.IsPostBack
block:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Name.Value = "6"
Label1.Text = Name.Value
End If
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I'm sure you can tell by now that I'm a beginner and I'm just starting to learn the difference between JS and VB. The snippet you posted goes on the html side but what does it replace? Would you mind placing it within my code posting so I can see the various parts? Thanks so much. (I know I have to take some courses in object oriented programming and web development in general but I'm just trying to accomplish a few things along the way.)
-
I'm sure you can tell by now that I'm a beginner and I'm just starting to learn the difference between JS and VB. The snippet you posted goes on the html side but what does it replace? Would you mind placing it within my code posting so I can see the various parts? Thanks so much. (I know I have to take some courses in object oriented programming and web development in general but I'm just trying to accomplish a few things along the way.)
No, the snippet I posted goes in the code-behind. It replaces your (currently empty)
Page_Load
event handler. You would also need to remove the Javascript block that I quoted, which resets the hidden input every time you click the button. ASPX page:<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="SIMPLE_VARIABLE_PASS_2.WebForm1" %>
<!DOCTYPE html>
<html>
<head runat="server">
<style type="text/css">
.auto-style1 {
position: absolute;
top: 79px;
left: 185px;
z-index: 1;
width: 80px;
}
</style>
</head>
<body style="height: 322px">
<form id="form2" runat="server"><!--THIS IS PART OF THE PASSING SETUP I sets up a variable "name" that can be passed to the codebehind VB.NET--> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:HiddenField ID="Name" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label" CssClass="auto-style1"></asp:Label>
</form>
</body>
</html>Code-behind:
Public Class WebForm1
Inherits System.Web.UI.PageProtected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Name.Value = "6" Label1.Text = Name.Value End If End Sub Protected Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click Name.Value = Name.Value + 1 Label1.Text = Name.Value End Sub
End Class
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
No, the snippet I posted goes in the code-behind. It replaces your (currently empty)
Page_Load
event handler. You would also need to remove the Javascript block that I quoted, which resets the hidden input every time you click the button. ASPX page:<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="SIMPLE_VARIABLE_PASS_2.WebForm1" %>
<!DOCTYPE html>
<html>
<head runat="server">
<style type="text/css">
.auto-style1 {
position: absolute;
top: 79px;
left: 185px;
z-index: 1;
width: 80px;
}
</style>
</head>
<body style="height: 322px">
<form id="form2" runat="server"><!--THIS IS PART OF THE PASSING SETUP I sets up a variable "name" that can be passed to the codebehind VB.NET--> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:HiddenField ID="Name" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label" CssClass="auto-style1"></asp:Label>
</form>
</body>
</html>Code-behind:
Public Class WebForm1
Inherits System.Web.UI.PageProtected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Name.Value = "6" Label1.Text = Name.Value End If End Sub Protected Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click Name.Value = Name.Value + 1 Label1.Text = Name.Value End Sub
End Class
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Got it. THANKS!!!
-
No, the snippet I posted goes in the code-behind. It replaces your (currently empty)
Page_Load
event handler. You would also need to remove the Javascript block that I quoted, which resets the hidden input every time you click the button. ASPX page:<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="SIMPLE_VARIABLE_PASS_2.WebForm1" %>
<!DOCTYPE html>
<html>
<head runat="server">
<style type="text/css">
.auto-style1 {
position: absolute;
top: 79px;
left: 185px;
z-index: 1;
width: 80px;
}
</style>
</head>
<body style="height: 322px">
<form id="form2" runat="server"><!--THIS IS PART OF THE PASSING SETUP I sets up a variable "name" that can be passed to the codebehind VB.NET--> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:HiddenField ID="Name" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label" CssClass="auto-style1"></asp:Label>
</form>
</body>
</html>Code-behind:
Public Class WebForm1
Inherits System.Web.UI.PageProtected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Name.Value = "6" Label1.Text = Name.Value End If End Sub Protected Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click Name.Value = Name.Value + 1 Label1.Text = Name.Value End Sub
End Class
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
IT WORKED! Thanks so much. It also makes so much more sense there. I'm an old Fortran / Basic / Cobal coder so I really need to study up on some more modern concepts. Thanks again though.