unit6.1

advertisement
MIS 3200 – Unit 6.1
• Moving between pages by redirecting
• Moving data between pages with session
variables
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.
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.”
– http://msdn.microsoft.com/en-us/library/ms178581.aspx
Declaring session variables
• Examples of declaring session variables:
notice the usage of brackets [] instead of squiggles {}
or parentheses () when working with session variables
Declaring session variables
• Session variables are objects
– i.e. they do not have a data type (e.g. decimal)
• Before using they need to either be converted
• Or Cast
Converting session variables
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 session variables again
• Since session variable are stored as objects, they must be
converted or cast before they can be used. Here is a casting
example:
Unit 6 L1
• Moving data between pages
1. Create two pages, one called lastnameU6L1_1.aspx and
another called lastnameU6L1_2.aspx
2. Add heading Unit 6 L1 - Session Variables (page 1) and Unit
6 L1 - Read Session Variables (page 2)
3. On the U6L1_1.aspx page, add a TextBox and a Button
4. In the button click method, assign the content of the
textbox to a session variable called contentsOfTextBox
5. Redirect the user to the 2nd page
L1 #2
6. On the U6L1_2.aspx page, in the Page_Load method:
a.
Test to see if the session variable is null
b.
If it is null, redirect the user back to the U61_1.aspx page
c.
Otherwise, build a string and assign the string to the label (you need to add
a label to this page)
7. 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:
L1 #3
8. Add a heading to reflect this is Unit 6 L1
9. Add appropriate comments to explain what the methods are doing
10. 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.
Time to try it out – Unit 6 L2
• Creating and reading session variables
1. 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
2. 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)
3. In U6L2_2.aspx, add an empty ListBox (which will hold the
selected fees that will be stored in a session variable)
a. Set the SelectionMode to Multiple
b. Set the Rows to 5
L2 #2
4. In the button click method of the Calculate sales tax and total create
two session variables,
a.
b.
c.
d.
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
5. In U6L2_2.aspx,
a.
b.
c.
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
L2 #3
6. 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)
7. Add appropriate comments to explain what the the
methods are doing
8. 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.
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