DataList中嵌入DataGrid

前台代码:
None.gif <% @ Page language = " c# "  Codebehind = " WebForm2.aspx.cs "  AutoEventWireup = " false "  Inherits = " program4.WebForm2 "   %>
None.gif
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm2 </ title >
None.gif        
< meta name = " GENERATOR "  Content = " Microsoft Visual Studio .NET 7.1 " >
None.gif        
< meta name = " CODE_LANGUAGE "  Content = " C# " >
None.gif        
< meta name = " vs_defaultClientScript "  content = " JavaScript " >
None.gif        
< meta name = " vs_targetSchema "  content = " http://schemas.microsoft.com/intellisense/ie5 " >
None.gif    
</ HEAD >
None.gif    
< body MS_POSITIONING = " GridLayout " >
None.gif        
< form id = " Form1 "  method = " post "  runat = " server " >
None.gif            
< FONT face = " 宋体 " >
None.gif                
< asp:DataList id = " DataList1 "  style = " Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px "  runat = " server " >
None.gif                    
< ItemTemplate >
None.gif                        
< TABLE cellSpacing = " 1 "  cellPadding = " 1 "  width = " 300 "  border = " 1 " >
None.gif                            
< TR >
None.gif                                
< TD >
None.gif                                    
< asp:Label id = lbl Runat = " server "  Text = ' <%# DataBinder.Eval(Container.DataItem,"分类名称")%> ' >
None.gif                                    
</ asp:Label ></ TD >
None.gif                            
</ TR >
None.gif                            
< TR >
None.gif                                
< TD >
None.gif                                    
< asp:DataGrid id = DataGrid1 runat = " server "  AutoGenerateColumns = " False "  Width = " 100% "  DataSource = ' <%# CreateTable((DataBinder.Eval(Container.DataItem,"图书分类_id")).ToString()) %> '  OnItemCommand = " dg_ItemCommand " >
None.gif                                        
< Columns >
None.gif                                            
< asp:BoundColumn Visible = " False "  DataField = " 图书信息_id "  HeaderText = " 图书信息_id " ></ asp:BoundColumn >
None.gif                                            
< asp:BoundColumn DataField = " 图书名称 "  HeaderText = " 图书名称 " ></ asp:BoundColumn >
None.gif                                            
< asp:BoundColumn DataField = " 作者 "  HeaderText = " 作者 " ></ asp:BoundColumn >
None.gif                                            
< asp:ButtonColumn Text = " 删除 "  CommandName = " Delete " ></ asp:ButtonColumn >
None.gif                                        
</ Columns >
None.gif                                    
</ asp:DataGrid ></ TD >
None.gif                            
</ TR >
None.gif                        
</ TABLE >
None.gif                    
</ ItemTemplate >
None.gif                
</ asp:DataList ></ FONT >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
后台代码:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  program4
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm2 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm2 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataList DataList1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Data_Bind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Data_Bind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection Cnn
=new SqlConnection("server=.;database=bookhome;uid=sa");
InBlock.gif            Cnn.Open();
InBlock.gif            SqlDataAdapter myAdapter
=new SqlDataAdapter("select * from 图书分类",Cnn);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            myAdapter.Fill(ds);
InBlock.gif            DataList1.DataSource
=ds.Tables[0].DefaultView;
InBlock.gif            DataList1.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
public DataView CreateTable(string id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection Cnn
=new SqlConnection("server=.;database=bookhome;uid=sa");
InBlock.gif            Cnn.Open();
InBlock.gif            SqlDataAdapter myAdapter
=new SqlDataAdapter("select * from 图书信息 where 图书分类_id="+id,Cnn);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            myAdapter.Fill(ds);
InBlock.gif            
return ds.Tables[0].DefaultView;
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_ItemCommand);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void dg_ItemCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.CommandName=="Delete")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlConnection Cnn
=new SqlConnection("Server=.;database=bookhome;uid=sa");
InBlock.gif                Cnn.Open();
InBlock.gif                SqlCommand Cmm
=new SqlCommand("delete from 图书信息 where 图书信息_id="+e.Item.Cells[0].Text,Cnn);
InBlock.gif                Cmm.ExecuteNonQuery();
InBlock.gif                Data_Bind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void DataGrid2_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/hwj/archive/2004/12/22/80639.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值