30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 20 ASP .<strong>NET</strong>, Web Forms and Web Controls 997<br />

21 ' determine if Session contains information<br />

22 If Session.Count 0 Then<br />

23<br />

24 ' iterate through Session values,<br />

25 ' display in ListBox<br />

26 For i = 0 To Session.Count - 1<br />

27<br />

28 ' s<strong>to</strong>re current key in sessionName<br />

29 keyName = Session.Keys(i)<br />

30<br />

31 ' use current key <strong>to</strong> display<br />

32 ' Session's name/value pairs<br />

33 booksListBox.Items.Add(keyName & _<br />

34 " <strong>How</strong> <strong>to</strong> <strong>Program</strong>. ISBN#: " & _<br />

35 Session(keyName))<br />

36 Next<br />

37 Else<br />

38 recommendationLabel.Text = "No Recommendations"<br />

39 booksListBox.Visible = False<br />

40 End If<br />

41 End Sub ' Page_Init<br />

42<br />

43 Private Sub Page_Load(ByVal sender As System.Object, _<br />

44 ByVal e As System.EventArgs) Handles MyBase.Load<br />

45<br />

46 ' Put user code <strong>to</strong> initialize the page here<br />

47 End Sub ' Page_Load<br />

48 End Class ' Recommendations<br />

Fig. 20.33 Session data read by an ASP .<strong>NET</strong> Web application <strong>to</strong> provide<br />

recommendations for the user (part 2 of 2).<br />

Event handler Page_Init (lines 13–41) retrieves the session information. If a user<br />

has not selected any language during any visit <strong>to</strong> this site, our Session object’s Count<br />

property will be zero. This property provides the number of session items contained in a<br />

Session object. If Session object’s Count property is zero (i.e., no language was ever<br />

selected) then we display the text No Recommendations.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!