CS 3870/CS 5870 Web User Controls Events (II)
Public Event Define event Raise event Handle event Create Event Handler Specify Event Handler
Test2 Event of User Control Run my TestTwo locally
Test2 Event works when one item becomes invalid. But may not work if multiple items are invalid.
Debugging Break Points Step Over Continue Stop
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
Generating Dynamic Pages Init Load State Process PostBack Data Load PostBack Events Save State Render Unload
Generating Dynamic Pages with User Controls Loading Page Loading User Controls Events triggered by loading user controls PostBack Events
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
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
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
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
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
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)
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
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
Web User Control Must Work Correctly At All Times! Prog 6 Web User Control Must Work Correctly At All Times!
Session Variables Display the previously selected Course Prog 6 Session Variables Display the previously selected Course
Prog 6 Copy Prog7 folder Paste as Prog6 folder Modify all files of Prog6 Make sure it works as Prog7 Then Work on Prog6!