问题是 - 新的应用程序,客户想要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'的属性。
答案 0 :(得分:1)
当您点击gridview中的“修改”时,Page_Load
事件会触发,您会看到LinqDataSourceMultiTable.TableName
是“类别”,因为这是您上次绑定的内容。如果要对gridview中显示的数据执行操作,则应再次重新绑定数据源。我认为没有必要执行所有清除部分。
答案 1 :(得分:0)
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
结束班