2011-08-16 21 views
0

新しいアプリケーション、顧客はLinq、.NET 4.0、多くのデータテーブル「カテゴリ」と「タイプ」 - 通常は2つのフィールド:IDと名前(または説明)複数のデータテーブルを編集するための単一のWebページ、LinqDataSource、GridView

これらのデータテーブルのそれぞれを編集するためにWebフォームを生成する代わりに、それらのすべてを編集するために1つのWebページが必要なだけでなく、必要なデータテーブル編集する。 (BTW - 「パターン・ジャンキー」はどこですか?これはパターンではありません。パターンはどこにありますか?)

私はNorthwindsデータベースで遊んでいます。ルックス背後

<%@ 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> 

コード:

DropDownConfigurationID Name   TableName PrimaryKeyName 
1      Categories  Categories CategoryID 
2      Regions   Regions RegionID 

asp.netのWebページには次のようになります。私は、次のデータで、このデータテーブルを埋めその後

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 

:私はテーブルと呼ばれるDropDownConfigurationを追加しましたlike:

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のいずれかがクリアされていないことです。 1つのデータテーブルの半分ともう半分のデータテーブルの種類だから私のコードの背後にある - 私はLinqDataSourceとGridViewを想像できるあらゆる方法でクリアしようとした。プログラムを実行する場合は、ドロップダウンリストボックスで "Regions"を選択し、 "regions"の最初の行を編集します。次のエラーが表示されます。

DataBinding: 'Category'には名前 'RegionID'。 説明:現在のWeb要求の実行中に、未処理の例外が発生しました。エラーの詳細とコード内のどこで発生したのかについては、スタックトレースを参照してください。

例外の詳細:System.Web.HttpException:DataBinding: 'Category'に 'RegionID'という名前のプロパティが含まれていません。

+0

::/が関係するデータベーステーブルを取得し、すべてのことは、単に2つのグリッドの代わりのものを作るためにこのコードを使用したい人のための新しいコードをポストだろうか?これは、20個のグリッドにデータが取り込まれるまで返済されません。 –

+0

私は編集が必要な少なくとも20のデータテーブルを持つことを期待しています – Bubba

答えて

1

グリッドビューで[編集]をクリックすると、Page_Loadイベントが発生し、LinqDataSourceMultiTable.TableNameが最後にバインドされたものであるため、「カテゴリ」であることがわかります。グリッドビューに表示されるデータに対してアクションを実行する場合は、データソースを再度バインドする必要があります。そのセクションをすべてクリアする必要はないと思います。

+0

多分私は十分に明確ではありませんでした。ドロップダウンリストで選択されたデータに基づいて、Page_Loadイベントのグリッドビューを再バインドします。 –

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 

エンドクラス

関連する問題