Tangara G

Tangara G

  • NA
  • 298
  • 90.3k

Dun understand why need to name the Session = something

Dec 26 2016 5:53 AM
Hi experts,
 
I refer to the below code.  Can someone explain to me why we need to name the Session = something ?
Specifically, this part :
 
  1. if(!IsPostBack)  
  2. {  
  3. if (Session["FirstName"] == null && Session["LastName"]==null)  
  4. {  
  5. Session["FirstName"] = "Aspdotnet";  
  6. Session["LastName"] = "  Suresh";  
  7. lblString.Text = "Welcome " + Session["FirstName"] + Session["LastName"];  

 And here's the full code
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionForm4.aspx.cs" Inherits="hiddenfieldtest.SessionForm4" %>  
  2.    
  3. <!DOCTYPE html>  
  4.    
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9.  <body>  
  10. <form id="form1" runat="server">  
  11. <div>  
  12. <h3>SessionStateData.aspx</h3>  
  13. <table>  
  14. <tr>  
  15. <td>FirstName:</td><td><asp:TextBox ID="txtfName" runat="server"/></td>  
  16. </tr>  
  17. <tr>  
  18. <td>LastName:</td><td><asp:TextBox ID="txtlName" runat="server"/></td>  
  19. </tr>  
  20. <tr><td></td><td> <asp:Button ID="btnSubmit" runat="server" Text="Set SessionState Data" OnClick="btnSubmit_Click" /></td></tr>  
  21. </table>  
  22. </div>  
  23. </form>  
  24. </body>  
  25. </html>  
  26.   
  27.   
  28.   
  29.   
  30. using System;  
  31. using System.Collections.Generic;  
  32. using System.Linq;  
  33. using System.Web;  
  34. using System.Web.UI;  
  35. using System.Web.UI.WebControls;  
  36.    
  37. namespace hiddenfieldtest  
  38. {  
  39.     public partial class SessionForm4 : System.Web.UI.Page  
  40.     {  
  41.         protected void Page_Load(object sender, EventArgs e)  
  42.         {  
  43.         }  
  44.         // Set Session values during button click  
  45.         protected void btnSubmit_Click(object sender, EventArgs e)  
  46.         {  
  47.             Session["FirstName"] = txtfName.Text;  
  48.             Session["LastName"] = txtlName.Text;  
  49.             Response.Redirect("Default2.aspx");  
  50.         }  
  51.     }  
  52. }  
  53.   
  54.   
  55.   
  56. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="hiddenfieldtest.Default2" %>  
  57.    
  58. <!DOCTYPE html>  
  59.    
  60. <html xmlns="http://www.w3.org/1999/xhtml">  
  61. <head runat="server">  
  62.     <title></title>  
  63. </head>  
  64.  <body>  
  65. <form id="form1" runat="server">  
  66. <div>  
  67. <h3>Default2.aspx</h3>  
  68. <table>  
  69. <tr>  
  70. <td colspan="2">Welcome <b><asp:Label ID="lblString" runat="server"/></b></td>  
  71. </tr>  
  72. <tr>  
  73. <td>Your FirstName: </td><td><b><asp:Label ID="lblfName" runat="server"/></b></td>  
  74. </tr>  
  75. <tr>  
  76. <td>Your LastName </td><td><b><asp:Label ID="lbllName" runat="server"/></b></td>  
  77. </tr>  
  78. <tr><td></td><td> </td></tr>  
  79. </table>  
  80. </div>  
  81. </form>  
  82. </body>  
  83. </html>  
  84.   
  85. using System;  
  86. using System.Collections.Generic;  
  87. using System.Linq;  
  88. using System.Web;  
  89. using System.Web.UI;  
  90. using System.Web.UI.WebControls;  
  91.    
  92. namespace hiddenfieldtest  
  93. {  
  94.     public partial class Default2 : System.Web.UI.Page  
  95.     {  
  96.         protected void Page_Load(object sender, EventArgs e)  
  97.         {  
  98.                
  99. if(!IsPostBack)  
  100. {  
  101. if (Session["FirstName"] == null && Session["LastName"]==null)  
  102. {  
  103. Session["FirstName"] = "Aspdotnet";  
  104. Session["LastName"] = "  Suresh";  
  105. lblString.Text = "Welcome " + Session["FirstName"] + Session["LastName"];  
  106. }  
  107. else  
  108. {  
  109. lblString.Text = Session["FirstName"]+" " + Session["LastName"];  
  110. lblfName.Text = Session["FirstName"].ToString();  
  111. lbllName.Text = Session["LastName"].ToString();  
  112. }  
  113. }  
  114. }  
  115.         }  
  116.     } 
 

Answers (9)