2011-08-16 49 views
0

問題是 - 新應用程序,客戶希望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'的屬性。

+0

我討厭這樣的設計:/捲入一個數據庫表和所有的只是爲了讓兩個網格,而不是一個?直到你有20個網格被填充後,這纔會有回報。 –

+0

我希望至少有20個需要編輯的數據表 – Bubba

回答

1

當您在gridview中點擊「編輯」時,Page_Load事件將觸發,您將看到LinqDataSourceMultiTable.TableName是「類別」,因爲這是您最後綁定的內容。如果你想對gridview中顯示的數據執行操作,你應該重新綁定你的數據源。我相信這並不需要做所有的清理部分。

+0

也許我還不夠清楚。根據在dropdownlist中選擇的數據重新綁定Page_Load事件中的gridview。 –

0

當它。問題非常明顯 - 我很尷尬地承認它。我的問題是,我沒有做標準的gridview事件處理程序 - 編輯,更新,編輯取消。將發佈新的代碼,任何人想要使用此代碼:

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 
    If ddlDropDownConfigurations.SelectedIndex > -1 Then 
     BindData() 
    End If 
End Sub 

Sub BindData() 
    Dim myDropDownConfiguration As DropDownConfiguration 
    Dim myDropDownConfigurationList As List(Of DropDownConfiguration) 

    ' 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 

    ' bind the GridView to the LinqDataSource with the new data table 
    GridViewMultiTable.DataKeyNames = New String() {myDropDownConfiguration.PrimaryKeyName} 
    GridViewMultiTable.DataBind() 
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 

Protected Sub GridViewMultiTable_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridViewMultiTable.RowEditing 
    GridViewMultiTable.EditIndex = e.NewEditIndex 
    BindData() 
End Sub 

Protected Sub GridViewMultiTable_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridViewMultiTable.RowCancelingEdit 
    GridViewMultiTable.EditIndex = -1 
    BindData() 
End Sub 

Protected Sub GridViewMultiTable_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewMultiTable.RowUpdating 
    GridViewMultiTable.EditIndex = -1 
    BindData() 
End Sub 

末級

相關問題