寂寞的风永不搁浅

学习之初 多为转载 未名出处 敬请见谅 点滴积累 必有作为

博客园 首页 新随笔 联系 订阅 管理
//aspx
<%@ Page language="c#" Codebehind="DataListNesting.aspx.cs" AutoEventWireup="false" Inherits="DataListNesting" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
  <title>DataListNesting</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">
  <script type="text/javascript">
  function showorhide(showID,imageID){
  if(document.getElementById(imageID).src == "http://localhost/WebDemo/images/down.gif")
  {
  if(document.getElementById(showID) != null)
  document.getElementById(showID).style.display = "none";
  document.getElementById(imageID).src = "http://localhost/WebDemo/images/up.gif";
  }
  else
  {
  if(document.getElementById(showID) != null)
  document.getElementById(showID).style.display = "block";
  document.getElementById(imageID).src = "http://localhost/WebDemo/images/down.gif";
  }
  // alert (showID);
  return false;
  }

  </script>
</HEAD>
<body>
  <form id="Form1" method="post" runat="server">
  <asp:datalist id="DataList1" runat="server" DataKeyField="au_id" BorderWidth="1px" GridLines="Both"
  CellPadding="4" BackColor="White" BorderStyle="None" BorderColor="#CC9966">
  <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
  <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
  <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
  <ItemTemplate>
  au_id:
  <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "au_id")%>'>
  </asp:Label><BR>
  au_lname:
  <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "au_lname")%>'>
  </asp:Label><BR>
  <asp:ImageButton id="ibtnUpDown" runat="server" CommandName="ibtnUpDown" ImageUrl="images/up.gif"></asp:ImageButton><BR>
  <asp:DataList id=DataList2 runat="server" BorderWidth="1px" GridLines="Both" CellPadding="4" BackColor="White" BorderStyle="None" BorderColor="#CC9966" DataKeyField="Title_id" DataSource='<%# GetTitleID(DataBinder.Eval(Container.DataItem, "au_id").ToString()) %>'>
  <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
  <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
  <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
  <ItemTemplate>
  title_id:
  <asp:Label id="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title_id")%>'>
  </asp:Label><BR>
  </ItemTemplate>
  <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
  </asp:DataList>
  </ItemTemplate>
  <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
  </asp:datalist></form>
</body>
</HTML>

//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.Data.SqlClient;

public class DataListNesting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;

private void BindList()
{
  SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs");
  SqlDataAdapter da = new SqlDataAdapter("select au_id,au_lname from authors", cn);
  DataSet ds = new DataSet();
  cn.Open();
  da.Fill(ds);
  cn.Close();
  DataList1.DataSource = ds;
  DataList1.DataBind();
}

public DataView GetTitleID(string au_id)
{
  SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs");
  SqlDataAdapter da = new SqlDataAdapter("select au_id, title_id from titleauthor where au_id = @au_id", cn);
  da.SelectCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = au_id;
  DataSet ds = new DataSet();
  cn.Open();
  da.Fill(ds);
  cn.Close();
  return ds.Tables[0].DefaultView;
}

private void Page_Load(object sender, System.EventArgs e)
{
  if(!IsPostBack)
  {
  BindList();
  }
}

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
  ImageButton ibtnUpDown;
  DataList DataList2;
  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
  {  
  DataList2 = (DataList) e.Item.FindControl("DataList2");
  if (DataList2 != null)
  DataList2.Attributes.Add("style", "display:none");
  ibtnUpDown = (ImageButton) e.Item.FindControl("ibtnUpDown");
  if(ibtnUpDown != null)
  ibtnUpDown.Attributes.Add("onclick","return showorhide('" + DataList2.ClientID + "','" + ibtnUpDown.ClientID + "');"); 
  }
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
  InitializeComponent();
  base.OnInit(e);
}

private void InitializeComponent()
{  
  this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
  this.Load += new System.EventHandler(this.Page_Load);
}
#endregion



posted on 2008-11-26 16:48  景阳  阅读(313)  评论(0)    收藏  举报