問題是 - 新應用程序,客戶希望Linq,.NET 4.0,許多數據表是「類別」和「類型」 - 簡單的數據表通常是兩個字段:ID和名稱(或說明)單個網頁,LinqDataSource,GridView編輯多個數據表
而不是生成一個Web窗體來編輯每個這些數據表 - 不需要一個網頁來編輯所有這些數據表,你只需選擇你想要的數據表編輯。 (順便說一句,「模式癮君子」在哪裏,是不是這個模式?這個模式的.NET代碼模板在哪裏?)
我正在玩Northwinds數據庫。我添加了一個表中調用DropDownConfiguration:
USE [Northwind]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DropDownConfiguration](
[DropDownConfigurationID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[TableName] [nvarchar](50) NOT NULL,
[PrimaryKeyName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_DropDownConfiguration] PRIMARY KEY CLUSTERED
(
[DropDownConfigurationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
然後我填入下面的數據該數據表:
DropDownConfigurationID Name TableName PrimaryKeyName
1 Categories Categories CategoryID
2 Regions Regions RegionID
asp.net web頁面的樣子:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="EditMultiTable.aspx.vb"
Inherits="EditMultiTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSourceMultiTable" runat="server" ContextTypeName="DataClassesDataContext"
EnableInsert="True" EnableUpdate="True" EntityTypeName="" TableName="Categories">
</asp:LinqDataSource>
<asp:DropDownList ID="ddlDropDownConfigurations" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:GridView ID="GridViewMultiTable" runat="server" DataKeyNames="CategoryID" DataSourceID="LinqDataSourceMultiTable">
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
向後看代碼如:
Option Explicit On
Option Strict On
Partial Class EditMultiTable
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myDropDownConfigurationList As List(Of DropDownConfiguration)
If Not Page.IsPostBack Then
' Tell the drop down what data tables we want to be able to edit
myDropDownConfigurationList = GetDropDownConfigurations()
ddlDropDownConfigurations.DataSource = myDropDownConfigurationList
ddlDropDownConfigurations.DataTextField = "Name"
ddlDropDownConfigurations.DataValueField = "DropDownConfigurationID"
ddlDropDownConfigurations.DataBind()
End If
End Sub
Protected Sub ddlDropDownConfigurations_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDropDownConfigurations.SelectedIndexChanged
Dim myDropDownConfiguration As DropDownConfiguration
Dim myDropDownConfigurationList As List(Of DropDownConfiguration)
If ddlDropDownConfigurations.SelectedIndex > -1 Then
' clear out the old data bindings between the LinqDataSource and the GridView
GridViewMultiTable.DataSourceID = Nothing
GridViewMultiTable.DataSource = Nothing
GridViewMultiTable.DataKeyNames = Nothing
GridViewMultiTable.DataMember = Nothing
GridViewMultiTable.AutoGenerateColumns = False
GridViewMultiTable.AutoGenerateEditButton = False
GridViewMultiTable.DataBind()
GridViewMultiTable.Columns.Clear()
' Set up the LinqDataSource for the new table
myDropDownConfigurationList = GetDropDownConfigurations()
myDropDownConfiguration = (From ddc In myDropDownConfigurationList Where ddc.DropDownConfigurationID = Long.Parse(ddlDropDownConfigurations.SelectedValue) Select ddc).FirstOrDefault()
LinqDataSourceMultiTable.TableName = myDropDownConfiguration.TableName
LinqDataSourceMultiTable.EntityTypeName = String.Empty
LinqDataSourceMultiTable.EnableInsert = True
LinqDataSourceMultiTable.EnableUpdate = True
LinqDataSourceMultiTable.DataBind()
' bind the GridView to the LinqDataSource with the new data table
GridViewMultiTable.DataSourceID = "LinqDataSourceMultiTable"
GridViewMultiTable.DataKeyNames = New String() {myDropDownConfiguration.PrimaryKeyName}
GridViewMultiTable.AutoGenerateColumns = True
GridViewMultiTable.AutoGenerateEditButton = True
GridViewMultiTable.DataBind()
End If
End Sub
' Get my data table that lists the data tables that I want to configure
Function GetDropDownConfigurations() As List(Of DropDownConfiguration)
Dim myDropDownConfigurationList As List(Of DropDownConfiguration)
Using myDataClassesDataContext As New DataClassesDataContext
myDropDownConfigurationList = (From ddc In myDataClassesDataContext.DropDownConfigurations Select ddc).ToList()
End Using
Return myDropDownConfigurationList
End Function
End Class
我的問題是 - LinqDataSource或GridView沒有被清除。它使用一個數據表的一半和另一半的數據表。這就是爲什麼在我的代碼背後 - 我試圖以各種可能的方式清除LinqDataSource和GridView。如果我運行該程序,請在下拉列表框中選擇「區域」,然後嘗試編輯「區域」的第一行。我得到以下錯誤:
DataBinding:'Category'不包含屬性名稱爲「RegionID」。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。
異常詳細信息:System.Web.HttpException:DataBinding:'Category'不包含名稱爲'RegionID'的屬性。
我討厭這樣的設計:/捲入一個數據庫表和所有的只是爲了讓兩個網格,而不是一個?直到你有20個網格被填充後,這纔會有回報。 –
我希望至少有20個需要編輯的數據表 – Bubba