Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio
  4. How to declare a session variable in VS2019 (HTML/CSS/JS with VB Code behind)

How to declare a session variable in VS2019 (HTML/CSS/JS with VB Code behind)

Scheduled Pinned Locked Moved Visual Studio
javascripthtmlcssdatabasedesign
6 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GungaDin16
    wrote on last edited by
    #1

    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.Page

    Protected 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

    Richard DeemingR 1 Reply Last reply
    0
    • G GungaDin16

      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.Page

      Protected 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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 the Page_Load event, inside an If 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      G 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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 the Page_Load event, inside an If 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

        G Offline
        G Offline
        GungaDin16
        wrote on last edited by
        #3

        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.)

        Richard DeemingR 1 Reply Last reply
        0
        • G GungaDin16

          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.)

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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.Page

          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
          
          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          G 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            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.Page

            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
            
            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

            G Offline
            G Offline
            GungaDin16
            wrote on last edited by
            #5

            Got it. THANKS!!!

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              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.Page

              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
              
              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

              G Offline
              G Offline
              GungaDin16
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups