分类:
2006-07-26 13:13:13
这个例子是来源于 asp.net start kit。
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="localtest.WebForm4" %>
<%# DataBinder.Eval(Container.DataItem,"au_lname") %>
<%# DataBinder.Eval(Container.DataItem,"au_fname") %>
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;
namespace localtest
{
///
/// WebForm4 的摘要说明。
///
public class WebForm4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList dgTopLevel;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindList();
}
}
public void Command_select(Object sender,DataListCommandEventArgs e)
{
string command = ((LinkButton)e.CommandSource).CommandName;
if(command == "close")
{
dgTopLevel.SelectedIndex = -1;
}
else
{
dgTopLevel.SelectedIndex = e.Item.ItemIndex;
}
BindList();
}
#region Web 窗体设计器生成的代码
#endregion
void BindList()
{
this.dgTopLevel.DataSource = this.GetTopList();
this.dgTopLevel.DataBind();
}
//第二层的数据源
public SqlDataReader GetSubList()
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=syd8gmt;database=pubs");
string strsql = "select top 5 au_fname from authors";
SqlCommand cmd = new SqlCommand(strsql,conn);
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
//第一层的数据源
public SqlDataReader GetTopList()
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=syd8gmt;database=pubs");
string strsql = " select top 10 au_lname from authors";
SqlCommand cmd = new SqlCommand(strsql,conn);
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
}
}