200 likes | 311 Views
CSCI 6962: Server-side Design and Programming. Data Viewing and Editing Tools in ASP. GridView. Basic tabular display Also simple field editing Basic steps: Create new SqlDataSource (as before) Drag in a GridView Set its Data Source. GridView. GridView now bound to database
E N D
CSCI 6962: Server-side Design and Programming Data Viewing and Editing Tools in ASP
GridView • Basic tabular display • Also simple field editing • Basic steps: • Create new SqlDataSource(as before) • Drag in a GridView • Set its Data Source
GridView • GridView now bound to database • By default, shows all fields in table form
GridView Editing • Can edit which fields shown and their format • Edit columns • Choose field to edit and manipulate its properties
GridView Editing • Example: • Make ISBN not visible (set Visibleto False) • Change Header text • Set alignment of Price to Right(under ItemStyle)
Template Fields • Can have more control by turning field into a Template • Edit from tasksand edit databindings for that field
Template Fields • Example: Binding Text format to Currency
Adding Command Elements • Example: Purchase button for each book • Enable Selection • Edit columns • Move to bottom • Set type to button • Set select text to “Add to Cart” • Convert to TemplateField
Adding Command Elements • Make sure each button knows ISBN of book selected • Bind ISBN to CommandArgument of the button • Passed to function when button press event occurs
Handing Selection Events • Must write code for GridView_RowCommand function • Called when select occurs on row of gridview • Extract ISBN from the event object e • Coerce it to original type if necessary
Editing in GridView • Can give users ability to UPDATE field values and DELETE records in database through GridView • Must connect to SQLDataSource with these abilities added • Add abilities in GridView tasks
Editing in Gridview • New “Command Field” column created • Can edit position/forms/names in Edit Columns
Editing in Gridview • May need to manually set queries in source • Initially ‘?’ inserted • May cause errors when DELETE or UPDATE actually run • Replace ‘?’ with @fieldname
Example: ASP.NET Shopping Cart • Customer adds book • Record added to Orderstable • Contains session ID • Cart shows books selected • Query by that session ID
Adding New Orders • SqlDataSource to get all books • Gridview linked to it • SqlDataSource to query for books with selected ISBN • ISBN stored in session • SqlDataSource to insert new record into Orders table
Adding New Orders Extract ISBN from selected row of Gridview and store in session Extract the Session ID to use to link to customer in Order table Execute query from ISBN stored in Session and extract title and price from (single) result Execute insert to put all session information into Orders table
Adding New Orders • Key idea: temporary storage of information into session fields • Can link queries/inserts to them (just like control fields) • May need to edit command to remove extra fields it finds in database (such as quantity, etc.) to match the query
Displaying the Cart • Link SQLDataSource to ID stored in session • Queries Orders for all records with that session ID
Displaying the Cart • Link Gridview to SqlDataSource • Add desired controls for delete, update • Add delete, update commands to SqlDataSource • Edit commands to use proper syntax
Event Handling for Errors • Updating a field causes an event • Done before commit, so can validate changes to database • Use controlname_ItemUpdating for UPDATEe.NewValues(“fieldname”) gets value updated at a field • e.Cancel = True cancels change if change invalid • Example: Validate quantity change to insure > 0