0

问题是 - 新应用程序,客户想要 Linq,.NET 4.0,许多“类别”和“类型”的数据表 - 通常是两个字段的简单数据表:ID 和名称(或描述)

与其生成一个 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 网页看起来像:

<%@ 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”的属性。

4

2 回答 2

1

当您在 gridview 中单击“编辑”时,该Page_Load事件将触发,您将看到LinqDataSourceMultiTable.TableName“类别”,因为这是您上次绑定的内容。如果您想对 gridview 中显示的数据执行操作,您应该再次重新绑定您的数据源。我相信没有必要执行所有清除部分。

于 2011-08-16T16:21:52.397 回答
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

结束类

于 2011-08-16T22:32:46.097 回答