ASP.NET Event Handlers Database -> Browser ->Shopping Basket Validators
Event Handlers An Event Handler is a subroutine that executes code for a given event In some cases you know when the event will happen – e.g. Page_Load Most of the time it is user driven – e.g. Button1_Click
Common handlers Onclick Onselect Onselectedindexchanged Onmouseover Onsubmit Can’t cope with overly complex calls
CommandEventArgs This is the key to determining which button was pressed inside an ASP.NET Form Allows you to use the properties CommandArgument CommandName More specialised versions available: DataListCommandEventArgs DataGridCommandEventArgs
Example 1 - In the Web Control
Example 1 - Event Handler Sub CommandButton_Click(sender As Object, e As CommandEventArgs) Select e.CommandName Case "Sort" Sort_List(CType(e.CommandArgument, String)) Case "Submit" Message.Text = "You clicked the Submit button" Case Else Message.Text = "Command name not valid." End Select End Sub
DataGrid CommandEventArgs ItemCommand – Generic event – raised whenever any button associated with an item in the DataGrid is clicked. – provides for programmatically determining which specific command button is clicked and take appropriate action – commonly used to handle custom command buttons for the DataGrid control. DeleteCommand, CancelCommand, EditCommand, UpdateCommand – Events raised whenever the corresponding cancel, delete, edit, or update button associated with an item in the DataGrid is clicked.
Example DataGrid delete event Sub Delete_Item(s As Object, e As DataGridCommandEventArgs ) objDT = Session("Cart") objDT.Rows(e.Item.ItemIndex).Delete() Session("Cart") = objDT End Sub <asp:buttoncolumn buttontype="LinkButton“ commandname="Delete" text="Remove Item" />
Example DataList Event 1 – in the web control NHS No.: Name : <asp:Button ID="btnAdd" runat="server" Text="Add” CommandName="Cart" />
Example DataList event 2 – event handler Sub mypatients_ItemCommand(s As Object, e As DataListCommandEventArgs) Handles mypatients.ItemCommand If e.CommandName = "Cart" Then ‘whole bunch of programming goes here demonstrated later End If End Sub
Database -> DataList -> Shopping Cart 1.Use a SELECT query to retrieve data and bind it to the datalist 2.Put a button in the datalist with CommandName=“Cart” 3.Write an event handler for the datalist – Takes information from the list – Puts information into new row of table stored in Session
Event handler for datalist Sub mypatients_ItemCommand(s As Object, e As DataListCommandEventArgs) Handles mypatients.ItemCommand If e.CommandName = "Cart" Then NOW WE WRITE THIS BIT End If End Sub
If CommandName=“Cart” 1.Retrieve the shopping cart from the session 2.Create a new row 3.Retrieve information from the relevant row of the datalist 4.Put the information from the datalist into the new row 5.Put the new row on the bottom of the cart table 6.Put the table back into the session This is the only new bit you have not seen before
– Displays on screen, but can’t be used programmatically <asp:Label id=“stuff” text=‘ ’ Runat=“server” /> – Now the info from the container is associated with an object that has an ID that we can use, e.g: stuff.Text
3 – retrieve data from relevant row of datalist If e.CommandName = "Cart" Then Dim temp as Label = e.Item.FindControl(“stuff") Dim quantity As TextBox = e.Item.FindControl(“amount”) End If So you put temp.Text and quantity.Text into the new row for the shopping cart
Explanation of code for step 3 e – DataListCommandEventArg that we passed in – Essentially all the objects from the row of the datalist that was clicked on e.Item – Used to address a specific object in the EventArgs e.Item.FindControl – Used to find a specific NAMED object in the EventArgs. In this case a specific label & a specific textbox
Images in the DataList <asp:Image ID=“mugshot” runat=“server” ImageUrl=' ‘ > Where P_Picture is a column in the table that contains the filename for the picture e.g. pic1.jpg
Compares the value of one input to another input or to a fixed value If the input is empty, the validation will succeed – You need to make the field required to solve this problem Compare Validator
Example Comparison Validation Enter your password: <asp:CompareValidator id="compval" Display="dynamic" ControlToValidate="txt1" ControlToCompare="txt2" ForeColor="red“BackColor="yellow" Type="String" EnableClientScript="false" Text="Validation Failed!" runat="server" />
Output
Range Validation Checks that the user enters a value that falls between two values Ranges can be within numbers, dates, or characters. Does not fail if the input is empty. – Solve this by making the input required Does not fail if the input value cannot be converted to the data type specified. – Combine with CompareValidator to solve this
Example Range Validation Enter a grade from 1 to 20: <asp:RangeValidator ControlToValidate="tbox1" MinimumValue="1“ MaximumValue=“20" Type="Integer“ EnableClientScript="false" Text="The value must be from 1 to 20" runat="server" />
Output
Regular Expression Validator Ensures that the value of an input matches a specified pattern Does not fail if the input is empty – Make the input required to solve this
Example Regular Expression Validation Enter your credit card number: <asp:RegularExpressionValidator id=“REV1" runat="server“ EnableClientScript="false" ControlToValidate="txtbox1" ErrorMessage="The card number must be 16 digits long" ValidationExpression="\d{16}“ />
Output
Required Field Validator Ensures user has input some data Leading and trailing spaces of the input value are removed before validation.
Example Required field validation Name: <asp:RequiredFieldValidator id="RFV1" runat="server" Text="Your address is required" ControlToValidate=" “ />
Output
Alternative Output
Other Validation Controls ValidationSummary – Displays a report of all validation errors occurred in a Web page CustomValidator – Write your own
Examples Code demonstrated in lecture will go on the ASP.NET section of the discussion board fvalidationcontrols.asp
TO DO Oasisplus – Shopping Basket Activity – Coursework NEXT WEEK – JavaScript