找了好久 datalist 嵌套的例子,找到了地址2那个算是很好的了,在公司做了很久就是不能实现 内层datalist 删除事件,
回家认真从头做一遍,发现确实可以,下面是我的一个测试的简单例子,供大家参考吧
WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="doubleDatalist.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td>
<asp:DataList id="dg1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:TextBox ID="txtorderID1" Runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.orderID")%>'>
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem,"ordertime")%>
</td>
</tr>
<tr>
<td>
<asp:DataList id="Dg2" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:TextBox ID="txtorderID" Runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.orderID")%>'>
</asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtorderproductID" Runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.orderproductID")%>'>
</asp:TextBox>
</td>
<td >
<asp:LinkButton ID="btnDel" Runat="server" CommandName="delete">删除</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</form>
</body>
</HTML>
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
namespace doubleDatalist
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList dg1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
BindDg1();
}
private void BindDg1()
{
SqlConnection myconn = new SqlConnection((string)System.Configuration.ConfigurationSettings.AppSettings["connStr"]);
string SQL = "select * from orders ";
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(SQL,myconn);
myconn.Open();
adapter.Fill(ds,"orders");
myconn.Close();
this.dg1.DataSource = ds.Tables[0].DefaultView;
this.dg1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.dg1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if ( e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
DataList Dg2 = (DataList)e.Item.FindControl("Dg2");
Dg2.ItemCommand += new DataListCommandEventHandler(Dg2_ItemCommand);
TextBox orderIDBox = (TextBox)e.Item.FindControl("txtorderID1");
string orderID = orderIDBox.Text.ToString();
Dg2.DataSource = GetSource(orderID).Tables[0];
Dg2.DataBind();
}
}
private DataSet GetSource(string orderID)
{
SqlConnection myconn = new SqlConnec