PROG 11044 Advanced Web Applications With NET Session
PROG 11044 Advanced Web Applications With. NET Session 9. 1 - Calendar and Validation Controls - Using Sessions
Review Postback The return trip from the server Page processing How it works (steps) How the first load is different from postback View. State Hidden __VIEWSTATE field Using hash array 9/30/2020 Wendi Jollymore, ACES 2
Calendar Control Properties: Selected. Date Contains a Date. Time object Set when user clicks a date on the control Can be initialized or set at run time Visible. Date The current month that’s visible To set, use the 1 st of the month Note that: Selected date can stay the same even as visible date changes 9/30/2020 Wendi Jollymore, ACES 3
Calendar Control Events Selection. Changed Occurs when selected date changes User clicks a day, month selector, or week selector Change Selelection. Mode property to allow selection of weeks and month Visible. Month. Changed Occurs when user navigates months Does not imply that selection changes! 9/30/2020 Wendi Jollymore, ACES 4
Date. Time Class Constructor: Date. Time(int year, int month, int day) Constructs a new Date. Time instance for the current year/month/day 9/30/2020 Wendi Jollymore, ACES 5
Date. Time Class Properties: Date Contains a Date. Time object for the current Date. Time instance Time set to midnight Day Contains the day of the month for the current Date. Time instance Int 32, 1 to 31 9/30/2020 Wendi Jollymore, ACES 6
Date. Time Class Properties: Day. Of. Week A Day. Of. Week enumeration value for the day of the week E. g Day. Of. Week. Sunday = 0 Month The month # for the current Date. Time instance 1 to 12 Year The year for the current Date. Time instance 9/30/2020 Wendi Jollymore, ACES 7
Date. Time Class Properties: Now Contains a Date. Time object representing the computer’s current date and time Static property Today Like Date. Time. Now, but without the time (midnight) 9/30/2020 Wendi Jollymore, ACES 8
Date. Time Class Methods: Add. Days(int days) Adds the specified number of days to the current Date. Time instance Returns a new Date. Time instance Add. Months(int months) As above, but adds specified number of months Add. Years(int years) As above, but adds specified number of years 9/30/2020 Wendi Jollymore, ACES 9
Date. Time Class Methods: Compare(Date. Time d 1, Date. Time d 2) Static method that takes two Date. Time objects and returns Int 32 Return value: Is less than 0 d 1 is earlier than d 2 Is greater than 0 d 1 is later than d 2 Is 0 d 1 and d 2 are the same date/time 9/30/2020 Wendi Jollymore, ACES 10
Date. Time Class Methods: Compare. To(Date. Time d 1) Non-static method compares d 1 with current Date. Time instance Returns Int 32, also: Less than 0 current instance is earlier than d 1 Greater than 0 current instance is later than d 1 Equal to 0 current instance and d 1 are the same 9/30/2020 Wendi Jollymore, ACES 11
Date. Time Class Methods: Is. Leap. Year(int year) Static method, returns boolean True of year is a leap year False if year isn’t a leap year To. String(), To. Short. Date. String(), To. Long. Date. String() Returns various String representations of the current Date. Time instance 9/30/2020 Wendi Jollymore, ACES 12
Exercise Add a Calendar control to a new page Use the Auto. Format item in the task menu to format your calendar You an customize by using the “Styles” category of properties in the Properties window Add code to page load to init the calendar to today’s date: Only if this is the first load i. e. Is. Postback == False! 9/30/2020 Wendi Jollymore, ACES 13
Calendar Tutorial Complete the Calendar tutorial in the notes (ASP. NET/Controls and Events) 9/30/2020 Wendi Jollymore, ACES 14
Validation Controls A set of controls that do client-side validation Controls are rendered as Java. Script Types of Controls: Compare. Validator Range. Validator Regular. Expression. Validator Required. Field. Validator Custom. Validator 9/30/2020 Wendi Jollymore, ACES 15
Validation Controls Common Properties: Display How the control is displayed on the page Static – error message part takes up space, even when not showing Dynamic – no space taken up by control when no error message None – don’t show error message at all Error. Message The error message to display Used by a Validation. Summary control 9/30/2020 Wendi Jollymore, ACES 16
Validation Controls Common Properties: Text The text the control will display on the page when there’s an error When this is empty, contents of Error. Message property are shown instead Control. To. Validate The ID of the control that this validator is validating Validation. Group Set to a group name if you have multiple groups of controls to validate separately 9/30/2020 Wendi Jollymore, ACES 17
Validation Controls Try it out… set this up: 9/30/2020 Wendi Jollymore, ACES 18
Validation Controls Drag a Required. Field. Validator to the right of the first text box ID: val. Req. Email Control. To. Validate: id of first email box Error. Message: “You must enter an email address. ” Display: Dynamic Load page and click button without typing email address. Add another of these validators for the “confirm email” text box (val. Req. Confirm) 9/30/2020 Wendi Jollymore, ACES 19
Validation Controls Add a Regular. Expression. Validator beside val. Req. Email ID: val. Good. Email Error. Message: “Your email address isn’t valid. ” Control. To. Validate: ID of email text box Display: Dyanmic Validation. Expression: Should be set to a regular expression Choose “Internet E-Mail Address” from the list 9/30/2020 Wendi Jollymore, ACES 20
Validation Controls Add a Compare. Validator beside val. Req. Confirm ID: val. Emails. Match Error. Message: “Your email address doesn’t match. ” Control. To. Validate: ID of confirm text box Display: Dyanmic Control. To. Compare: Should be the ID of the control you want to compare to – set to ID of email text box Value. To. Compare property is used if you want to compare to a static value, instead 9/30/2020 Wendi Jollymore, ACES 21
Exercise Do the Validation exercises in the notes ASP. NET / Controls and Events 9/30/2020 Wendi Jollymore, ACES 22
Sessions What is a Session? The time a visitor spends on the web site Starts when they first loads a page Ends when they leave your site, or the session automatically expires Identified by a unique ID stored in a session cookie Cookieless sessions can be used by storing id in the URL You can store information in the Session object A hash table Can store entire objects, not just data Session data is stored on server 9/30/2020 Wendi Jollymore, ACES 23
Sessions You can store data in a session like you would in View. State: Session[“user. Name”] = “Kaluha”; The session variable is called user. Name The value of the user. Name variable is “Kaluha” 9/30/2020 Wendi Jollymore, ACES 24
Sessions Tutorial Do the Sessions Tutorial in the notes ASP. NET / Maintaining State / Sessions Download the images! Set up the page as instructed 9/30/2020 Wendi Jollymore, ACES 25
Sessions Tutorial Repeater control: Repeats a set of markup/objects You format the templates and it repeats them You bind the control to a data source It will fill the template with your data values The repeater will be linked to a hash table of items and quantities Book title will be key Quantity will be value 9/30/2020 Wendi Jollymore, ACES 26
Sessions Tutorial <Item. Template> … </Item. Template> Defines each “row item” in the repeater control Each row in the control will be as defined in the template 9/30/2020 Wendi Jollymore, ACES 27
Sessions Tutorial <asp: Label ID="Label 1" width="70" runat="server" text='<%#. . . %>'> </asp: Label> <%# %> tags are for inline data binding expressions Single quotes around text attribute value because inline expression requires double -quotes The expression will be evaluated and result will appear in this label’s Text property 9/30/2020 Wendi Jollymore, ACES 28
Sessions Tutorial ((Dictionary. Entry)Container. Data. Item) Container. Data. Item Refers to a specific item of data bound to the current container Current container is the repeater template Data Item will be an element in the hash table Dictionary. Entry is a class Models a piece of data with a key/value pair Has a Key property that contains the key portion, and Value property for value Cast Data. Item into Dictionary. Entry so we can use the Key and Value properties 9/30/2020 Wendi Jollymore, ACES 29
Sessions Tutorial basket. List. Data. Source = basket. Table; basket. List. Data. Bind(); Data. Source property The source of the data that will fill this control Can be a hash table, data set, and other stuff you’ll learn later in the course Data. Bind() method Gets the data from the data source and populates the control 9/30/2020 Wendi Jollymore, ACES 30
Sessions Tutorial Do the portion of the tutorial that takes you through the code Session[“basket”] = basket. Table; Stores a reference to the basket. Table hash table in the Session variable basket Page_Prerender() event Executes after control events have been executed, but before HTML is rendered We need the results from the event handler in order to update the repeater Page_Load is too soon, as it executes before the event handler 9/30/2020 Wendi Jollymore, ACES 31
For Next Class. . That last tutorial introduced Data Binding We’ll look at this in more detail in the next class 9/30/2020 Wendi Jollymore, ACES 32
- Slides: 32