Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP.NET Event Handlers Database -> Browser ->Shopping Basket Validators.

Similar presentations


Presentation on theme: "ASP.NET Event Handlers Database -> Browser ->Shopping Basket Validators."— Presentation transcript:

1 ASP.NET Event Handlers Database -> Browser ->Shopping Basket Validators

2 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

3 Common handlers Onclick Onselect Onselectedindexchanged Onmouseover Onsubmit Can’t cope with overly complex calls

4 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

5 Example 1 - In the Web Control

6 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

7 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.

8 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" />

9 Example DataList Event 1 – in the web control NHS No.: Name : <asp:Button ID="btnAdd" runat="server" Text="Add” CommandName="Cart" />

10 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

11 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

12 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

13 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

14 – 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

15 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

16 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

17 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

18 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

19 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" />

20 Output

21 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

22 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" />

23 Output

24 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

25 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}“ />

26 Output

27 Required Field Validator Ensures user has input some data Leading and trailing spaces of the input value are removed before validation.

28 Example Required field validation Name: Email: <asp:RequiredFieldValidator id="RFV1" runat="server" Text="Your E-mail address is required" ControlToValidate="email“ />

29 Output

30 Alternative Output

31 Other Validation Controls ValidationSummary – Displays a report of all validation errors occurred in a Web page CustomValidator – Write your own

32 Examples Code demonstrated in lecture will go on the ASP.NET section of the discussion board http://wwww.w3schools.com/aspnet/aspnet_re fvalidationcontrols.asp

33 TO DO Oasisplus – Shopping Basket Activity – Coursework NEXT WEEK – JavaScript


Download ppt "ASP.NET Event Handlers Database -> Browser ->Shopping Basket Validators."

Similar presentations


Ads by Google