Download presentation
Presentation is loading. Please wait.
1
CS 3870/CS 5870 Web User Controls Events (II)
2
Public Event Define event Raise event Handle event
Create Event Handler Specify Event Handler
3
Test2 Event of User Control Run my TestTwo locally
4
Test2 Event works when one item becomes invalid.
But may not work if multiple items are invalid.
5
Debugging Break Points Step Over Continue Stop
6
Dynamic Pages User request a dynamic page
Request a page (first time) Post back Revisit a page ASP.NET generates the requested page User receives a new page
7
Generating Dynamic Pages
Init Load State Process PostBack Data Load PostBack Events Save State Render Unload
8
Generating Dynamic Pages with User Controls
Loading Page Loading User Controls Events triggered by loading user controls PostBack Events
9
Other Items Could Be Invalid
Private Sub EventHandler(ByVal item As TestTwo_CourseAndCredit, ByVal valid As Boolean) If Not valid Then txtTotal.Text = "" Exit Sub End If For index = 0 To bag.Count - 1 c1 = bag.GetByIndex(index) If c1.CourseID = item.CourseID Then c1.Credits = item.Credits total += c1.Credits Next txtCredits.Text = total End Sub
10
CourseAndCredit.ascx Partial Class TestTwo_CourseAndCredit Inherits System.Web.UI.UserControl ‘ Private data Private _courseID, _courseName As String ‘Private _credits As Integer Private _credits As String Private _valid As Boolean = True Private _message As String = “” End Class
11
User Control Load Event
Protected Sub TestTwo_CourseAndCredit_Load (. . .) Handles Me.Load txtID.Text = _courseID txtName.Text = _courseName txtCredits.Text = _credits lblMsg.Text = _message End Sub
12
Define Event Partial Class Prog7_ShoppingItem . . .
‘Public Event CreditsChanged(ByVal item As ‘ TestTwo_CourseAndCredit, ByVal valid As Boolean) Public Event CreditsChanged(ByVal item As TestTwo_CourseAndCredit) End Class
13
Raise Event ‘ Update the object and raise event
Protected Sub txtCredits_TextChanged(…) Handles … ‘ update _valid ‘ update message ‘ update _credits ‘ display message RaiseEvent CreditsChanged(Me) End Sub
14
Raise Event Dim credits As Integer
If Integer.TryParse(txtCredits.Text, credits) Then If credits <= 0 Then _message = "Not positive" _valid = False ElseIf credits > 5 Then _message = "Larger than 5" Else _message = "" _valid = True End If _message = "Not an integer" _credits = txtCredits.Text lblMessage.Text = _message RaiseEvent CreditsChanged(Me)
15
Add Event Handler Protected Sub Page_Load(. . .) Handles Me.Load c1 = CType(LoadControl("../CourseAndCredit.ascx"), TestTwo_CourseAndCredit) AddHandler c1.CreditsChanged, AddressOf HandleChangeEvent theForm.Controls.Add(c1) End Sub
16
Private Sub HandleChangeEvent(ByVal Item
Private Sub HandleChangeEvent(ByVal Item ) Dim allValid as Boolean = True Dim total As Integer = 0 For i = 0 To bag.Count - 1 c = bag.GetByIndex(i) If c.theID = item.theID Then ‘ update the item in the bag End If If c.Valid Then total += c.theCredits Else allValid = False Next If allValid Then txtTotal.Text = total txtTotal.Text = “” End Sub
17
Web User Control Must Work Correctly At All Times!
Prog 6 Web User Control Must Work Correctly At All Times!
18
Session Variables Display the previously selected Course
Prog 6 Session Variables Display the previously selected Course
19
Prog 6 Copy Prog7 folder Paste as Prog6 folder Modify all files of Prog6 Make sure it works as Prog7 Then Work on Prog6!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.