Lien Huynh COMS 463 – Presentation Nov 18, 2005 WEB FORM CONTROLS: ADROTATOR, CALENDAR, AND DATA VALIDATION I. ADROTATOR The Adrotator displays advertisement banners on a Web form. The display ad is randomly changed each time the form is loaded or refreshed. The AdRotator control uses an XML file to create a list of ad information. The XML file must begin and end with an <Advertisements> tag. Inside the <Advertisements> tag, there may be several <Ad> tags, which define each ad. The predefined elements inside the <Ad> tag are listed below: <ImageUrl>: The path to the image file (optional). <NavigateUrl>: The URL to link to when the user clicks the ad (optional). <AlternateText>: An alternate text for the image (optional). <Keyword>: A category for the ad (optional). <Impressions>: The weighting that affects how often an ad is displayed compared to other ads (optional) 1. Create a list of advertisement: Following is the XML file that creates a list of advertisement <Advertisements> <Ad> <ImageUrl>pink orchid.jpg</ImageUrl> <NavigateUrl>http://www.mnsu.edu</NavigateUrl> <AlternateText>Advertisement banner</AlternateText> <Impressions>1</Impressions> </Ad> <Ad> <ImageUrl>tulip.jpg</ImageUrl> <NavigateUrl>http://www.google.com</NavigateUrl> <AlternateText>More Advertisement Banner</AlternateText> <Impressions>1</Impressions> </Ad> <Ad> <ImageUrl>daisy.jpg</ImageUrl> <NavigateUrl>http://www.yahoo.com</NavigateUrl> <AlternateText>Last Advertisement Banner</AlternateText> <Impressions>1</Impressions> </Ad> </Advertisements> 2. To select ads using the AdCreated event The following Visual Basic code shows how to set the ImageUrl, NavigateUrl, and AlternateText properties of an AdRotator (Adrotator1) control in the AdCreated event handler: a. First, create an AdCreated event handler for the control. The second parameter passed in the handler contains a reference to the ad being created. b. Then, set properties of the ad object to specify the image and navigation URLs of the ad Public Sub AdRotator1_AdCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AdCreatedEventArgs) Handles AdRotator1.AdCreated e.ImageUrl = "rose.jpg" e.NavigateUrl = "http://www.hotmail.com" e.AlternateText = "Let’s take a rest" End Sub II. CALENDAR: The calendar Web server control displays a calendar. User can navigate to any day in any year. In Microsoft Windows, the range of valid dates is January 1, 100 A.D. through December 31, 9999 A.D. Users can select a single date, a week, a month, or contiguous dates. Noncontiguous dates are selected by programming. User can display appointment or other information in a calendar. 1. Design: a. Selection mode: Day ( default): user can select only a single day DayWeek: user can select a single day or an entire week DayWeekMonth: user can select a single day, an entire week, or the entire month b. FirstDayOfWeek: It is used for choosing the day that starts a week. The default is Sunday. c. Specify week and month selection link text: by setting the SelectWeekText and SelectMonthText Properties. The default value is "&gt;" you will get this sign (>) for the link in the design view. d. Types of dates Today’s date: By default, it is the current date on the server. You can adjust it to get the local date Visible date: what month appears in the calendar. It can be changed by programming Selected date or dates: selected by users. 2. To specify that a single day is selected In a method for DayRender event, by getting information from the Day object in the DayRenderEvent argument, the day that is selected is determined. Query the Date property of the Day object, and then set that day's IsSelectable property to true. The following code shows that the date November 25, 2005 is selected; all other dates are not. Public Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender Dim MaryBirthday As Date = New Date(2000, 11, 25) If (e.Day.Date = MaryBirthDay) Then e.Day.IsSelectable = True Else e.Day.IsSelectable = False End If End Sub 3. To determine how many dates are selected Get the value of the Count property of the SelectedDates collection. Following is the code: Public Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged TextBox1.Text = "You selected " & Calendar1.SelectedDates.Count.ToString() & " date(s)" End Sub 4. To get the date range of a multi-date selection Get the count of selected dates, get the first date selected, and then get the last date selected (count -1). Following is the code displaying the range. Public Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged With Calendar1.SelectedDates If .Count > 1 Then Dim firstDate As Date = .Item(0).Date Dim lastDate As Date = .Item(.Count - 1).Date TextBox1.Text = firstDate.ToString() TextBox2.Text = lastDate.ToString() End If End With End Sub 5. Select multiple non-contiguous dates Use Add method. Uniqueness is automatically enforced, that means adding the same date is ignored. Following is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click With Calendar1.SelectedDates .Clear() .Add(New Date(2005, 11, 2)) .Add(New Date(2005, 11, 9)) .Add(New Date(2005, 11, 16)) .Add(New Date(2000, 11, 23)) End With End Sub The clear() method clears selected dates. 6. Highlight Following is the code showing highlighted weekends: Sub DayRender(source As Object, e As DayRenderEventArgs) If Not e.Day.IsOtherMonth And Not e.Day.IsWeekend Then e.Cell.BackColor = System.Drawing.Color.Yellow End If End Sub Following is the code showing that today’s date is highlighted. Sub DayRender(source As Object, e As DayRenderEventArgs) If e.Day.IsToday Then e.Cell.BackColor = System.Drawing.Color.Yellow e.Cell.ForeColor = System.Drawing.Color.Red End If End Sub III. DATA VALIDATION CONTROL Data validation guarantees that the data in you application are correct and accurate. Data validation can be designed by several approaches: user interface code, application code, or data constraints. There are several types of data validation: Data type validation: verify data type. For example, the IsNumeric(expression) returns True if the expression is a number; otherwise, it returns False. Range checking: For example, set data that only allows the alphabetic letters A through Z. All other characters would not be valid. Code checking: Typically requires a lookup table. For example, maybe your application calculates sales tax for only certain state codes. You would need to create a validation table to hold the authorized, taxable state codes. This validation table could be part of a business rule, or it could be implemented directly in the database for query lookup. Complex validation: is used when simple field and lookup validation is not enough. 1. Data validation tools using in web forms: CompareValidator (Comparison to a value): Compares a user’s entry against a constant value (less than, equal, greater than, and so on), for example, checking for invalid Password. CustomValidator (User-defined): Checks the data entered by the user using validation logic from a custom method that you write – process on the server or client RangeValidator (Range checking): Checks that data enter by the user falls between specified lower and upper boundaries. Check range within pairs of numbers, alphabetic characters, and dates. RegularExpressionValidator (Pattern matching): Check that entry matches a pattern defined by a regular expression. For example, checking the predictable sequences of social security numbers, e-mail addresses, telephone numbers, postal codes, or IP address. RequiredFieldValidator (Required Entry) Ensure that the user does not skip an entry. ValidationSummary does not perform validation, but it is often used in conjunction with the controls listed above. The purpose of the ValidationSummary control is to display the error messages from all the validation controls on the page together, in a single location. 2. When Validation Occurs Validation occurs after page initialization (that is, after view-state and postback data have been processed) but before Change or Click event handlers are called. You can perform the validation procedure earlier by calling the Validate() method during the page load. Following is the code for range checking using the Validate() method: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'If the date entered in a text box is out of range (startDate-endDate) 'RangeValidator displays an error Dim startDate As Date = New Date(2005, 12, 1) Dim endDate As Date = New Date(2005, 12, 30) RangeValidator1.MinimumValue = startDate RangeValidator1.MaximumValue = endDate RangeValidator1.Type = ValidationDataType.Date RangeValidator1.Validate() If Not RangeValidator1.IsValid Then RangeValidator1.ErrorMessage = "The registered date must be in the range of the start date and end dates." End If 'Put user code to initialize the page here End Sub Another example shows programmatically checking if data type correct or not: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ''Put user code to initialize the page here ' Check data type if it is a date type or not Dim Date1, Date2 As Date Dim NotADate As String Dim CheckTypeDate As Boolean Date1 = "February 12, 1969" Date2 = #2/12/1969# NotADate = "Hello" CheckTypeDate = IsDate(Date1) TextBox6.Text = Date1 & " " & CheckTypeDate ' Returns True. CheckTypeDate = IsDate(Date2) TextBox7.Text = Date2 & " " & CheckTypeDate ' Returns True. CheckTypeDate = IsDate(NotADate) TextBox8.Text = NotADate & " " & CheckTypeDate ' Returns False. End Sub REFERENCES Microsoft Visual Studio. NET, ms-help://MS.VSCC.2003. http://www.w3.school Ulman,Chris; Kauffman, John; Hart, Christ; Sussman, David. (2004). Beginning ASP.NET 1.1 with VB.NET 2003. Indiana: Wiley Publishing, Inc QUESTIONS A. Short Answer 1. What is an adrotator? 2. What is the output of the following code (very brief answer) Public Sub AdRotator1_AdCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AdCreatedEventArgs) Handles AdRotator1.AdCreated e.ImageUrl = "rose.jpg" e.NavigateUrl = "http://www.hotmail.com" e.AlternateText = "Let’s take a rest" End Sub 3. What is the output of the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click With Calendar1.SelectedDates .Clear() .Add(New Date(2005, 11, 2)) .Add(New Date(2005, 11, 9)) .Add(New Date(2005, 11, 16)) .Add(New Date(2000, 11, 23)) End With End Sub 4. What is the function of RequireFieldValidator? 5. List three of web form controls that you know. B. Multiple Choice 1. To create a list of advertisements for an Adrotator, the following file is used: a. *.HTML file b. *.XML file c. *.asp file d. *.aspx.vb file 2. User can select a single day by setting which property below: a. Day b. DayWeek. c. DayWeekMonth d. All of above 3. The XML file must begin and end with a. an <Advertisements> tag. b. an <ad> tag c. a <ImageUrl> tag d. a <NavigateUrl> tag e. a <Keyword> tag 4. You cannot select non-contiguous dates in a calendar control a. True b. False 5. Which of the followings is not a data validation controls: a. CompareValidator b. RangeValidator c. RegularExpressionValidator d. RequireFieldValidator e. None of above C. Fill in the Blank 1. ________________ tag shows the weighting that affects how often an ad is displayed compared to other ads. 2. ________________: Validator that checks if the data entered by the user falls between specified lower and upper boundaries. Check range within pairs of numbers, alphabetic characters, and dates. 3. _________________is a Web control guarantees that the data in you application are correct and accurate. 4. The _____________ method is used to clear a selected date in calendar control. 5. The IsNumeric(expression) returns True if the expression is a number; otherwise, it returns False. It is _______ ________ validation SOLUTIONS A. Short Answers 1. An adrotator displays advertisement banners on a Web form. 2. An adrotator 3. The dates of November, 2, 9, 16, and 23, 2005 are selected. 4. Ensures that user does not skip an entry 5. TextBox, calendar, and adrotator (They can be DropDownList, radio button, etc…) B. Multiple Choice 1. b 2. d 3. a 4. b 5. e C. Solutions for 5 Fill in the Blank Questions 1. <Impressions> 2. RangeValidator 3. Data Validation Control 4. clear() 5. Data type