Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data-Driven - 1 © Minder Chen, 2002-2007 Building Data-Driven Web Sites in ASP.NET 2.0.

Similar presentations


Presentation on theme: "Data-Driven - 1 © Minder Chen, 2002-2007 Building Data-Driven Web Sites in ASP.NET 2.0."— Presentation transcript:

1 Data-Driven - 1 © Minder Chen, 2002-2007 Building Data-Driven Web Sites in ASP.NET 2.0

2 Data-Driven - 2 © Minder Chen, 2002-2007 Codeless Data-Binding Process Source: http://beta.asp.net/QUICKSTART/aspnet/doc/ctrlref/data/default.aspxhttp://beta.asp.net/QUICKSTART/aspnet/doc/ctrlref/data/default.aspx

3 Data-Driven - 3 © Minder Chen, 2002-2007 Data Bound Controls DataGrid DataList GridView DetailsView FormView Repeater New in ASP.NET 2.0

4 Data-Driven - 4 © Minder Chen, 2002-2007 Data-Bound Controls

5 Data-Driven - 5 © Minder Chen, 2002-2007 Data Bound Controls and Data Source Controls

6 Data-Driven - 6 © Minder Chen, 2002-2007 Create a New Data Source Drag and drop the AccessDataSource Web server control on to a Web form. Click the smart tab of the AccessDataSource1 to select common tasks for the AccessDataSource Click on the Configure Data Source… task Smart tag icon Smart tag menu

7 Data-Driven - 7 © Minder Chen, 2002-2007 Select the Access database

8 Data-Driven - 8 © Minder Chen, 2002-2007 Define a SQL (Select) statement SELECT [CategoryID], [CategoryName], [Description] FROM [Categories] Don't choose any query name if you need to create a data source that will be used to insert, update, or delete data in the database.

9 Data-Driven - 9 © Minder Chen, 2002-2007 Test Query

10 Data-Driven - 10 © Minder Chen, 2002-2007 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">

11 Data-Driven - 11 © Minder Chen, 2002-2007 CategoryList.aspx

12 Data-Driven - 12 © Minder Chen, 2002-2007 Connecting Data Source and GridView Drop and drop a GridView to the Web form From the GridView smart tag menu, click on the dropdown listbox -- Choose Data Source and then select AccessDataSource1

13 Data-Driven - 13 © Minder Chen, 2002-2007 Edit Columns…

14 Data-Driven - 14 © Minder Chen, 2002-2007 Edit Columns…

15 Data-Driven - 15 © Minder Chen, 2002-2007 Create a HyperLinkField for the CategoryName Column Delete the CategoryName from the Selected Fields list box Highlight HyperlinkField and click Add Move the HyperLinkField to the position between CategoryID and Description in the Selected Fields list box Set up the properties of the HyperlinkField

16 Data-Driven - 16 © Minder Chen, 2002-2007 Set up HyperlinkField Set up the Hyperlink field so that it displays CategoryName as the link words, and links to ProductsByCategory.aspx while passing two QueryString variables cid and cname where cid contains CategoryID and cname contains CategoryName ProductsByCategory.aspx?cid={0}&cname={1} CategoryID, CategoryName CategoryID CategoryName

17 Data-Driven - 17 © Minder Chen, 2002-2007 Source Code <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1"> <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" /> <asp:HyperLinkField DataNavigateUrlFields="CategoryID, CategoryName" DataNavigateUrl FormatString = " ProductsByCategory.aspx?cid={0}&cname={1}" DataTextField="CategoryName" HeaderText="Category Name" /> <asp:BoundField DataField="Description" H HeaderText="Description" SortExpression="Description" />

18 Data-Driven - 18 © Minder Chen, 2002-2007 CategoryList.aspx Untitled Page Category List <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1"> <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" /> <asp:HyperLinkField DataNavigateUrlFields="CategoryID, CategoryName" DataNavigateUrlFormatString="ProductsByCategory.aspx?cid={0}&cname={1}" DataTextField="CategoryName" HeaderText="Category Name" /> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">

19 Data-Driven - 19 © Minder Chen, 2002-2007 ProductsByCategory.aspx

20 Data-Driven - 20 © Minder Chen, 2002-2007 Choose Data Source Edit Data Source

21 Data-Driven - 21 © Minder Chen, 2002-2007 Define the SQL Statement SELECT [ProductID], [ProductName], [Discontinued], [UnitPrice] FROM [Products] WHERE ([CategoryID] = ?) Make sure you choose Products table

22 Data-Driven - 22 © Minder Chen, 2002-2007 Add Where Clause

23 Data-Driven - 23 © Minder Chen, 2002-2007 Parameterized Commands Parameter properties –Parameterized database commands Example: –Get value for WHERE clause in SelectCommand from QueryString parameter or item selected in a drop-down list Web server control Example: –Get value for WHERE clause in DeleteCommand from GridView

24 Data-Driven - 24 © Minder Chen, 2002-2007 Parameters Properties NameDescription SelectParametersSpecifies parameters for SelectCommand InsertParameters UpdateParameters DeleteParameters FilterParametersSpecifies parameters for FilterExpression Specifies parameters for InsertCommand Specifies parameters for UpdateCommand Specifies parameters for DeleteCommand

25 Data-Driven - 25 © Minder Chen, 2002-2007 Parameter TypesNameDescription ControlParameter Binds a replaceable parameter to a control property CookieParameter Binds a replaceable parameter to a cookie value` FormParameter Binds a replaceable parameter to a form field ProfileParameter Binds a replaceable parameter to a profile property QueryStringParameterBinds a replaceable parameter to a query string parameter Parameter Binds a replaceable parameter to a data field SessionParameter Binds a replaceable parameter to a session variable

26 Data-Driven - 26 © Minder Chen, 2002-2007 Test Query

27 Data-Driven - 27 © Minder Chen, 2002-2007

28 Data-Driven - 28 © Minder Chen, 2002-2007 AutoFormat

29 Data-Driven - 29 © Minder Chen, 2002-2007 TemplateField

30 Data-Driven - 30 © Minder Chen, 2002-2007 Code for TemplateField '> '>

31 Data-Driven - 31 © Minder Chen, 2002-2007 Edit Template

32 Data-Driven - 32 © Minder Chen, 2002-2007 Edit Template

33 Data-Driven - 33 © Minder Chen, 2002-2007 Set Up Text property

34 Data-Driven - 34 © Minder Chen, 2002-2007 Set up NavigateUrl property ProductRecord.aspx?pid={0}

35 Data-Driven - 35 © Minder Chen, 2002-2007 End Template Editing

36 Data-Driven - 36 © Minder Chen, 2002-2007 ProductsByCategory.aspx Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) LabelCategoryID.Text = Request.QueryString("cid") LabelCategoryName.Text = Request.QueryString("cname") End Sub Products by a Category CategoryID: Catgeory Name:

37 Data-Driven - 37 © Minder Chen, 2002-2007 Continued… <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" > <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> <asp:TemplateField HeaderText="Product Name" SortExpression="ProductName"> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl=' ' Text=' '> <asp:TextBox ID="TextBox1" runat="server" Text=' '>

38 Data-Driven - 38 © Minder Chen, 2002-2007 Continued… <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" /> <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}" > <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

39 Data-Driven - 39 © Minder Chen, 2002-2007 Continued… <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" SelectCommand="SELECT [ProductID], [ProductName], [Discontinued], [UnitPrice] FROM [Products] WHERE ([CategoryID] = ?)"> <asp:QueryStringParameter DefaultValue="1" Name="CategoryID" QueryStringField="cid" Type="Int32" />

40 Data-Driven - 40 © Minder Chen, 2002-2007 ProductRecord.aspx Runtime

41 Data-Driven - 41 © Minder Chen, 2002-2007 Define the SQL Statement SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice], [UnitsInStock], [Discontinued] FROM [Products] WHERE ([ProductID] = ?)

42 Data-Driven - 42 © Minder Chen, 2002-2007 Add Where Clause

43 Data-Driven - 43 © Minder Chen, 2002-2007 Use DetailsView (One Record at a Time)

44 Data-Driven - 44 © Minder Chen, 2002-2007 Format the DetailsView

45 Data-Driven - 45 © Minder Chen, 2002-2007 ProductRecord.aspx Untitled Page Product Record <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="ProductID" DataSourceID="AccessDataSource1" Font-Bold="False" ForeColor="#333333" GridLines="None" Height="50px"> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> <asp:BoundField DataField="ProductName" ItemStyle-Width="200px" HeaderText="ProductName" SortExpression="ProductName" /> <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />

46 Data-Driven - 46 © Minder Chen, 2002-2007 Continued… <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="UnitPrice" SortExpression="UnitPrice"> <asp:BoundField DataField="UnitsInStock" DataFormatString="{0} in stock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" /> <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" /> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice], [UnitsInStock], [Discontinued] FROM [Products] WHERE ([ProductID] = ?)"> <asp:QueryStringParameter Name="ProductID" QueryStringField="pid" Type="Int32" />

47 Data-Driven - 47 © Minder Chen, 2002-2007 Manage Categories

48 Data-Driven - 48 © Minder Chen, 2002-2007

49 Data-Driven - 49 © Minder Chen, 2002-2007 Define the SQL Statement

50 Data-Driven - 50 © Minder Chen, 2002-2007 Use Optimistic Concurrency Control <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = ? AND [CategoryName] = ? AND [Description] = ?" InsertCommand="INSERT INTO [Categories] ([CategoryID], [CategoryName], [Description]) VALUES (?, ?, ?)" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName] = ?, [Description] = ? WHERE [CategoryID] = ? AND [CategoryName] = ? AND [Description] = ?" ConflictDetection="CompareAllValues">

51 Data-Driven - 51 © Minder Chen, 2002-2007 Without Optimistic Concurrency Control <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = ?" InsertCommand="INSERT INTO [Categories] ([CategoryID], [CategoryName], [Description]) VALUES (?, ?, ?)" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName] = ?, [Description] = ? WHERE [CategoryID] = ?">

52 Data-Driven - 52 © Minder Chen, 2002-2007 CategoryUpdateGridView.aspx Untitled Page Manage Categories <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1" AllowPaging="True" PageSize="5" AllowSorting="True" > <asp:CommandField ButtonType="Button" HeaderText="Actions" ShowDeleteButton="True" ShowEditButton="True" /> <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" /> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" /> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />

53 Data-Driven - 53 © Minder Chen, 2002-2007 Continued… <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID] = ? AND [CategoryName] = ? AND [Description] = ?" InsertCommand="INSERT INTO [Categories] ([CategoryID], [CategoryName], [Description]) VALUES (?, ?, ?)" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName] = ?, [Description] = ? WHERE [CategoryID] = ? AND [CategoryName] = ? AND [Description] = ?" ConflictDetection="CompareAllValues">

54 Data-Driven - 54 © Minder Chen, 2002-2007 Change Description Field to Template Field

55 Data-Driven - 55 © Minder Chen, 2002-2007 Choose Edit Template

56 Data-Driven - 56 © Minder Chen, 2002-2007 Change EditItemTemplate

57 Data-Driven - 57 © Minder Chen, 2002-2007 for Description <asp:TemplateField HeaderText="Description" SortExpression="Description"> <asp:Label ID="Label1" runat="server" Text=' '> <asp:TextBox ID="TextBox1" runat="server" Text=' '>

58 Data-Driven - 58 © Minder Chen, 2002-2007 Change TextBox TextMode Property from SingleLine to MultiLine

59 Data-Driven - 59 © Minder Chen, 2002-2007 CategoryUpdateGridView2.aspx

60 Data-Driven - 60 © Minder Chen, 2002-2007 CategoryProductMasterDetail.aspx

61 Data-Driven - 61 © Minder Chen, 2002-2007 1 2

62 Data-Driven - 62 © Minder Chen, 2002-2007 CategoryProductMasterDetail.aspx Protected Sub DropDownList1_SelectedIndexChanged( ByVal sender As Object, ByVal e As System.EventArgs ) LabelCategory.Text = "Category ID selected: " & DropDownList1.SelectedItem.Value LabelCategory.Text &= " Category Name selected: " & DropDownList1.SelectedItem.Text End Sub Untitled Page <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="AccessDataSource1" DataTextField="CategoryName" DataValueField="CategoryID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">

63 Data-Driven - 63 © Minder Chen, 2002-2007 CatgeoryProductMasterDetail2.aspx

64 Data-Driven - 64 © Minder Chen, 2002-2007

65 Data-Driven - 65 © Minder Chen, 2002-2007

66 Data-Driven - 66 © Minder Chen, 2002-2007 CategoryProductMasterDetail3.aspx

67 Data-Driven - 67 © Minder Chen, 2002-2007 CategoryProductMasterDetail3.aspx http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd Untitled Page <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="AccessDataSource1" DataTextField="CategoryName" DataValueField="CategoryID"AppendDataBoundItems=true> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~~/App_Data/northwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]" CancelSelectOnNullParameter=false>

68 Data-Driven - 68 © Minder Chen, 2002-2007 Continued… <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="AccessDataSource2"> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" /> <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~~/App_Data/northwind.mdb" SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [UnitPrice], [UnitsInStock], [Discontinued] FROM [Products] WHERE ([CategoryID] = ?)"> <asp:ControlParameter ControlID="DropDownList1" Name="CategoryID" PropertyName="SelectedValue" Type="Int32" />

69 Data-Driven - 69 © Minder Chen, 2002-2007 ManageProduct.aspx

70 Data-Driven - 70 © Minder Chen, 2002-2007 Add Edit and New Button 1 2 3 4

71 Data-Driven - 71 © Minder Chen, 2002-2007 1 2 3 Make sure that Insert, Update, Delete statements are generated

72 Data-Driven - 72 © Minder Chen, 2002-2007 Insert Problem Using Visual Web Developer 2005 and ASP.NET 2.0 If you insert an AccessDataSource that use a table for insert, update and delete operations and the table has a primary key that is an autonumber, the code that is generated is shown in the next 3 slides. If you want to insert a record (Using a DetailsView object) you get the following error: "You tried to assign the Null value to a variable that is not a Variant data type.". Bold & UnderlinedTo correct the problem, remove the highlighted (Bold & Underlined) code that has been generated. Source: http://forums.asp.net/1027200/ShowPost.aspx

73 Data-Driven - 73 © Minder Chen, 2002-2007 ManageProduct.aspx Untitled Page Manage Product Information <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="ProductID" DataSourceID="AccessDataSource1" Height="50px" > <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" /> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />

74 Data-Driven - 74 © Minder Chen, 2002-2007 Continued… <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = ?" InsertCommand="INSERT INTO [Products] [ProductID], ( [ProductID], [ProductName], [CategoryID], [UnitPrice], [Discontinued], [SupplierID], [QuantityPerUnit], [UnitsInStock], [UnitsOnOrder], [ReorderLevel]) ?, VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice], [Discontinued], [SupplierID], [QuantityPerUnit], [UnitsInStock], [UnitsOnOrder], [ReorderLevel] FROM [Products]" UpdateCommand="UPDATE [Products] SET [ProductName] = ?, [CategoryID] = ?, [UnitPrice] = ?, [Discontinued] = ?, [SupplierID] = ?, [QuantityPerUnit] = ?, [UnitsInStock] = ?, [UnitsOnOrder] = ?, [ReorderLevel] = ? WHERE [ProductID] = ?">

75 Data-Driven - 75 © Minder Chen, 2002-2007 Continued… Delete!!!

76 Data-Driven - 76 © Minder Chen, 2002-2007 Data Binding In Templates Goal: Simpler data expression syntax for both simple and complex expressions V1 Databinding syntax – New Syntax – // one-way databinding – // two-way databinding

77 Data-Driven - 77 © Minder Chen, 2002-2007 Data Binding in Templates Two-way data bindings –Enable templated controls to retrieve input values to use as parameters –Allow templated controls to perform automatic updates/deletes –GridView and DetailsView both support two- way data bindings using TemplateField –New FormView control allows completely templated rendering of DetailsView –Future ListView control would provide this behavior for DataList functionality

78 Data-Driven - 78 © Minder Chen, 2002-2007 Use Template for Editing CategoryID

79 Data-Driven - 79 © Minder Chen, 2002-2007 CategoryID Template ….. <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> <asp:Label ID="Label1" runat="server" Text=' '> <asp:TextBox ID="TextBox1" runat="server" Text=' '> <asp:TextBox ID="TextBox1" runat="server" Text=' '> <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" /> …..

80 Data-Driven - 80 © Minder Chen, 2002-2007

81 Data-Driven - 81 © Minder Chen, 2002-2007 Set SelectedValue Properties

82 Data-Driven - 82 © Minder Chen, 2002-2007 Modified Template '> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="AccessDataSource1" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue=' '> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/northwind.mdb" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CategoryID") %>'>

83 Data-Driven - 83 © Minder Chen, 2002-2007 Indirect Events Indirect events can be raised by ASP.NET internally because of some action taken by the user. Deleting Is deleting allowed Cancel the event e.Cancel = True Deleted No Yes Run the event ' Handle exception gracefully If e.Exception IsNot Nothing Then AccessDataSource1_Deleted AccessDataSource1_Deleting

84 Data-Driven - 84 © Minder Chen, 2002-2007 Handle Exception (Partial Code) Sub CustomersGridView_RowDeleted(ByVal sender As Object, ByVal e As GridViewDeletedEventArgs) ' Use the Exception property to determine whether an exception ' occurred during the delete operation. If e.Exception Is Nothing Then ' Use the AffectedRows property to determine whether the ' record was deleted. Sometimes an error might occur that ' does not raise an exception, but prevents the delete ' operation from completing. If e.AffectedRows = 1 Then Message.Text = "Record deleted successfully." Else Message.Text = "An error occurred during the delete operation." End If Else ' Insert the code to handle the exception. Message.Text = "An error occurred during the delete operation." ' Use the ExceptionHandled property to indicate that the ' exception is already handled. e.ExceptionHandled = True End If End Sub

85 Data-Driven - 85 © Minder Chen, 2002-2007 GridViewDeletedEventArgs Example <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" datakeynames="CustomerID" onrowdeleted="CustomersGridView_RowDeleted" runat="server"> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring = " " runat="server">

86 Data-Driven - 86 © Minder Chen, 2002-2007 Insert Issues Not pass an identity column during an insert to the database? –Set InsertVisible property of control to false After the insert, retrieve the newly inserted record, complete with identity column? –Uses Inserted event of DataSource control and grab the value of the identity column using e.Command.Parameters collection

87 Data-Driven - 87 © Minder Chen, 2002-2007 Insert, Update, Delete Issues – Keep Controls in Sync Refresh one control when another changes? Call DataBind() method of second control from first control’s ItemInserted,ItemUpdated, or ItemDeleted event Protected Sub FormView1_ItemInserted(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) If e.Exception IsNot Nothing Then LabelMsg.Text = "Insert failed. Reason: " & e.Exception.Message Else LabelMsg.Text = "Insert succeeded!" FormView1.Visible = False GridView1.DataBind() End If End Sub

88 Data-Driven - 88 © Minder Chen, 2002-2007 Set a default value of a control for an insert Use DataBound event, checking the CurrentMode property Protected Sub FormView1_DataBound(ByVal sender As Object, _ ByVal e As System.EventArgs) ' Set default value of the supplier ID to the current supplier ID If (FormView1.CurrentMode = FormViewMode.Insert) Then CType(FormView1.FindControl("SUPIDLabel"), Label).Text = _ LabelSupplierID.Text End If End Sub


Download ppt "Data-Driven - 1 © Minder Chen, 2002-2007 Building Data-Driven Web Sites in ASP.NET 2.0."

Similar presentations


Ads by Google