datalist 中嵌套datalist 以及内層datalist 操作

找了好久 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(order

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值