利用GridView显示主细表并添加打开、关闭功能

本文例子使用嵌套的 GridView 来显示主细表,并使用 JavaScript 来控制明细表的显示与隐藏。值得注意的是:在 GridView 的 RowDataBound 的事件里,不要多次执行数据库的打开,否则,将很快会导致连接数已满的问题。

例子中的数据库,请参照《 ASP.NET 2.0应用开发技术》一书中附带的光盘中的数据库。

查看例子

代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewNested.aspx.cs" Inherits="Exam_GridViewNested" %><br /><!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /> <head runat="server"><br /> <title>利用GridView显示主细表并添加打开、关闭功能</title><br /> <style type="text/css"><br /> td,div,a {font-size:12px}<br /> </style><br /> <script type="text/javascript"> //<![cdata[ function ShowHidden(sid,ev) { ev = ev || window.event; var target = ev.target || ev.srcElement; var oDiv = document.getElementById("div" + sid); oDiv.style.display = oDiv.style.display == "none"?"block":"none"; target.innerHTML = oDiv.style.display == "none"?"显示":"隐藏"; } //]]> </script><br /> </head><br /> <body><br /> <form id="form1" runat="server"><br /> <asp:gridview ID="MasterGridView" runat="server" AutoGenerateColumns="false" Width="760px"<br /> BorderWidth="1" OnRowDataBound="MasterGridView_RowDataBound" DataKeyNames="id"<br /> ShowHeader="false"><br /> <columns><br /> <asp:templatefield><br /> <itemtemplate><br /> <div style="width: 100%; padding: 2px; font-weight: bold; background-color: #DEDEDE;<br /> float: left"><br /> <span style="float: left">栏目名称:<%#eval("title") %></span><span style="float: right;<br /> color: Red; cursor: pointer" onclick="ShowHidden('<%#eval("id") %>',event)">隐藏</span></div><br /> <div style="background-color: #FFF; padding-left: 60px;clear:both" id="div<%#eval("id") %>"><br /> <asp:gridview ID="DetailGridView" runat="server" AutoGenerateColumns="false" ShowHeader="true"<br /> Width="100%" HorizontalAlign="left"><br /> <headerstyle BackColor="#9999FF" /><br /> <columns><br /> <asp:templatefield HeaderText="文章名称"><br /> <itemtemplate><br /> <a href="/article/<%#eval("objectguid") %>/read.aspx"><br /> <%#eval("title") %><br /> </a>[<%# Eval("HitCount") %>]<br /> </itemtemplate><br /> </asp:templatefield><br /> <asp:boundfield HeaderText="发布日期" DataField="CreateDate" HtmlEncode="false" DataFormatString="{0:yyyy年MM月dd日}"<br /> ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Center" /><br /> </columns><br /> </asp:gridview><br /> </div><br /> </itemtemplate><br /> </asp:templatefield><br /> </columns><br /> </asp:gridview><br /> </form><br /> </body><br /></html><br />

CS:

using System;<br />using System.Data;<br />using System.Data.OleDb;<br />using System.Configuration;<br />using System.Collections;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.HtmlControls;<br />public partial class Exam_GridViewNested : System.Web.UI.Page<br />{<br />string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aspxWeb.mdb;Persist Security Info=True";<br />OleDbConnection cn1;<br />protected void Page_Load(object sender, EventArgs e)<br />{<br />if (!Page.IsPostBack)<br />{<br />OleDbConnection cn = new OleDbConnection(ConnectionString);<br />cn.Open();<br />cn1 = new OleDbConnection(ConnectionString);<br />cn1.Open();<br />OleDbCommand cmd = new OleDbCommand("select * from [Subject]", cn);<br />OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);<br />MasterGridView.DataSource = dr;<br />MasterGridView.DataBind();<br />dr.Close();<br />cmd.Dispose();<br />cn.Dispose();<br />cn1.Dispose();<br />cn = cn1 = null;<br />}<br />}<br />protected void MasterGridView_RowDataBound(object sender, GridViewRowEventArgs e)<br />{<br />if (e.Row.RowType == DataControlRowType.DataRow)<br />{<br />GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");<br />if (oGridView != null)<br />{<br />OleDbCommand cmd = new OleDbCommand("select top 10 * from Document Where pid = " + MasterGridView.DataKeys[e.Row.RowIndex].Value, cn1);<br />OleDbDataReader dr1 = cmd.ExecuteReader();<br />oGridView.DataSource = dr1;<br />oGridView.DataBind();<br />dr1.Close();<br />cmd.Dispose();<br />}<br />}<br />}<br />}<br />

VB.NET:

Private ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aspxWeb.mdb;Persist Security Info=True"<br />Private cn1 As OleDbConnection<br />Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)<br />If Not Page.IsPostBack Then<br />Dim cn As OleDbConnection = New OleDbConnection(ConnectionString)<br />cn.Open<br />cn1 = New OleDbConnection(ConnectionString)<br />cn1.Open<br />Dim cmd As OleDbCommand = New OleDbCommand("select * from [Subject]", cn)<br />Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)<br />MasterGridView.DataSource = dr<br />MasterGridView.DataBind<br />dr.Close<br />cmd.Dispose<br />cn.Dispose<br />cn1.Dispose<br />cn = cn1 = Nothing<br />End If<br />End Sub<br />Protected Sub MasterGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)<br />If e.Row.RowType = DataControlRowType.DataRow Then<br />Dim oGridView As GridView = CType(e.Row.FindControl("DetailGridView"), GridView)<br />If Not (oGridView Is Nothing) Then<br />Dim cmd As OleDbCommand = New OleDbCommand("select top 10 * from Document Where pid = " + MasterGridView.DataKeys(e.Row.RowIndex).Value, cn1)<br />Dim dr1 As OleDbDataReader = cmd.ExecuteReader<br />oGridView.DataSource = dr1<br />oGridView.DataBind<br />dr1.Close<br />cmd.Dispose<br />End If<br />End If<br />End Sub<br />

你可能感兴趣的:(GridView)