Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIS 3200 – Unit 6.1 Moving between pages by redirecting

Similar presentations


Presentation on theme: "MIS 3200 – Unit 6.1 Moving between pages by redirecting"— Presentation transcript:

1 MIS 3200 – Unit 6.1 Moving between pages by redirecting
Moving data between pages with session variables

2 Page redirection If we want to send a user to a different page, we can do this using Response.Redirect. For example: Before a user is re-directed to a new page, we have to consider what happens to any data entered on the page and how to move that data to the new page. One way we can address this problem is by using session state and storing session variables in the session state.

3 Session State – What is it?
“ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session.”

4 Declaring session variables
Examples of declaring session variables: notice the usage of brackets [] instead of squiggles {} or parentheses () when working with session variables

5 Using session variables
Before Session Variables can be used, they must be converted to the data types they were before.

6 Casting session variables
Considered as describing an object as a web control The Session[“lbSelectedItems”] had to be created on a previous page (similar to slide 5) before this casting example would work. Casting takes an object (in this example, a session variable) and allows us to describe the object as a specific type of object (e.g. a ListBox). Once the object has been cast, it can be assigned to a variable of the casted data type. For example: This allows the contents of the session variable to now reside in a ListBox where we can treat it like any other ListBox control we have worked with. Note, the Session[“lbSelectedItems”] had to be created on a previous page (similar to slide 5) before this casting example would work.

7 Casting session variables
Since session variable are stored as objects, they must be converted or cast before they can be used. Here is a casting example:

8 Unit 6 L1 Moving data between pages
Create two pages, one called lastnameU6L1_1.aspx and another called lastnameU6L1_2.aspx Add heading Unit 6 L1 - Session Variables (page 1) and Unit 6 L1 - Read Session Variables (page 2) On the U6L1_1.aspx page, add a TextBox and a Button In the button click method, assign the content of the textbox to a session variable called contentsOfTextBox Redirect the user to the 2nd page

9 L1 #2 On the U6L1_2.aspx page, in the Page_Load method:
Test to see if the session variable is null or the text is blank If it is null or blank, redirect the user back to the U61_1.aspx page Otherwise, build a string and assign the string to the label (you need to add a label to this page) Add a button on the U6L1_2.aspx page that resets the session variable (set it to null) and redirects the user back to the 1st page:

10 L1 #3 Add a heading to reflect this is Unit 6 L1
Add appropriate comments to explain what the methods are doing Create a link to your U6L1 page from your MIS3200 page and copy everything to ASPNET and submit your MIS Portfolio URL to the drop box.

11 Time to try it out – Unit 6 L2
Creating and reading session variables Copy your Unit5L2.2.aspx file to your Unit 6 folder and rename it to lastnameU6L2_1.aspx and create a 2nd page called lastnameU6L2_2.aspx Update the heading to be Unit 6 L2 – Creating Session Variables (in the 1st file) and Unit 6 L2 – Reading Session Variables (in the 2nd file) In U6L2_2.aspx, add an empty ListBox (which will hold the selected fees that will be stored in a session variable) Set the SelectionMode to Multiple Set the Rows to 5

12 L2 #2 In the button click method of the Calculate sales tax and total create two session variables, one will store the contents of the selected ListBox one will store the contents of the invoice label your existing “clean-up” code can be commented out redirect the user to U6L2_2.aspx In U6L2_2.aspx, add a label above the ListBox and set the Text property to You selected the following fees: add a label below the ListBox to hold the invoice add a button with the Text set to Make a new selection and in the button click method, reset both session variables and redirect the user back to the 1st page

13 L2 #3 Test the Make a new selection functionality by trying to run the 2nd page directly from within Web Developer – this should redirect the user to the U6L2_1.aspx page (since the session variables are set to null – you will need to test the session variables in the page_load to make sure they exist and if not, send them to the U6L2_1.aspx page) Add appropriate comments to explain what the the methods are doing Create a link to your U6L2_1.aspx page from your MIS3200 page and copy everything to ASPNET and submit your MIS Portfolio URL to the drop box.

14 Think About It! Why do we need to move data between pages?
What does casting do? How do you cast a session variable as a label?


Download ppt "MIS 3200 – Unit 6.1 Moving between pages by redirecting"

Similar presentations


Ads by Google