GridView嵌套GridView,对子GridView及父GridView级联删除的问题,使用SqlDataSource删除版本
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewNesting.aspx.cs" Inherits="GridViewNesting" %>
<!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>GridViewNesting</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="au_id" >
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%# Eval("au_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="au_lname">
<ItemTemplate>
<asp:Label id="Label2" runat="server" Text='<%# Eval("au_lname")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="titleDetail">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="au_id,title_id" DataSourceID="SqlDataSource2">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="title_id" >
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%# Eval("title_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="au_ord">
<ItemTemplate>
<asp:Label id="Label2" runat="server" Text='<%# Eval("au_ord")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=./sqlexpress;Initial Catalog=pubs;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="select au_id, title_id, au_ord from titleauthor where au_id = @au_id" DeleteCommand="delete from titleauthor where au_id = @au_id and title_id = @title_id">
<SelectParameters>
<asp:Parameter Name="au_id" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="au_id" />
<asp:Parameter Name="title_id" />
</DeleteParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=./sqlexpress;Initial Catalog=pubs;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="select au_id, au_lname from authors" DeleteCommand="delete from titleauthor where au_id = @au_id;delete from authors where au_id = @au_id">
<DeleteParameters>
<asp:Parameter Name="au_id" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridViewNesting : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlDataSource sqlDataSource2;
if (e.Row.RowType == DataControlRowType.DataRow)
{
sqlDataSource2 = e.Row.FindControl("SqlDataSource2") as SqlDataSource;
if (sqlDataSource2 != null)
{
sqlDataSource2.SelectParameters["au_id"].DefaultValue = (e.Row.DataItem as DataRowView)["au_id"].ToString();
}
}
}
}
aspx