Title: ASP.NET Components
1ASP.NET Components
Original slides by Kathleen Kalata Dave
Turton Modified by Meyer Tanuan
2Maintaining Table with Many Columns
- Showing many columns (gt7) in a DataGrid is
difficult for users - Hands-on Assignment 6 Customer Maintenance
- Create a Web page to view a few columns
- Create a separate Web page to insert, update and
delete a record. - When calling the second Web page, pass the
primary key as a Query String Parameter
3HyperLink Column in a DataGrid
- To jump to another Web page
- Add Hyperlink column
- If using query parameter
- set the URL field and URL format string ()
4MTComponent WebForm1.aspx
5.NET Components
- A Component is a class containing one or more
functions - These are typically compiled as separate
assemblies (i.e., Class Library) for reuse - Assemblies (i.e., DLLs) can be used by other .NET
languages
6Coding a Componentadd new item ? component class
Add namespaces you expect to use
7Using a Component
Try Dim CatList As MTComponent.Ch8Products
CatList New MTComponent.Ch8Products '
sample code for DL and DataReader
MyCatList.DataSource CatList.GetCat()
MyCatList.DataBind() ' sample code for DG
and DataReader DataGrid1.DataSource
CatList.GetCat() DataGrid1.DataBind()
Dim CatID As Integer CInt(Request.Params("CatID"
)) lblMessage.Text "CatId parameter value
" CatID.ToString Catch ex As Exception
lblMessage.Text ex.Message End Try
Declare a Component object
Create instance of component
Call components instance method
8Exception Handling
- Use Try / Catch / Finally Blocks
- Code that may create a problem is placed in the
try block - Code to deal with the problem (the exception
handler) is placed in catch blocks - Within a Component, use System.Diagnostics.Debug.W
riteLine() to display in VS.NET Output panel - Code to be executed whether an exception is
thrown or not is placed in the finally block
9Exception Handling Sample Code
Imports System.Diagnostics
Try SqlConnection1.Open()
Dim objCM As SqlCommand objCM New
SqlCommand(SQL, SqlConnection1) Dim
objDR As SqlDataReader objDR
objCM.ExecuteReader(CommandBehavior.CloseConnectio
n) Return objDR Catch ex As
Exception Debug.Write("GetCat ")
Debug.WriteLine(ex.Message)
Throw ex Finally SqlConnection1.Close()
-- if using DataSet End Try
10Extra Using Session object
- Session object maintains state across multiple
Web page requests - To Store
- Session(customer) aCustomer
- To Access
- Dim aCustomer As XXCustomer
- aCustomer Session(customer)
- Hands-on Assignment 6
- Reference VS.NET Help Session State