源码位置:http://download.csdn.net/detail/weinierbian/4426673
我们先要配置好一个sqldatasource,假设为sqldatasource1,再添加一个GridView,假设为GridView1,把AutoGenerateSelectButton="True", DataSourceID="SqlDataSource1"
再添加一个sqldatasource,假设为sqldatasource2,在配置Select语句的时候,选择Where子句,做如下配置
添加一个DetailsView,假设为DetailsView1, 把DataSourceID="SqlDataSource2"
这样就可以实现选择GridView1的一行,在DetailsView1显示详细内容了。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2"
DataKeyNames="StuID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None"
PageSize="5" AutoGenerateSelectButton="True">
<FooterStyle BackColor="Tan" />
<Columns>
<asp:BoundField DataField="StuID" HeaderText="StuID" SortExpression="StuID"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Stuname" HeaderText="Stuname" SortExpression="Stuname" />
<asp:BoundField DataField="Stuage" HeaderText="Stuage" SortExpression="Stuage" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
</Columns>
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
<br />
数据行详细信息:<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cxConnectionString %>"
SelectCommand="SELECT [StuID], [Stuname], [Stuage], [Grade] FROM [student]"></asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource2"
GridLines="Vertical" Height="50px" Width="325px">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="StuID" HeaderText="StuID" SortExpression="StuID">
<ControlStyle Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="Stuname" HeaderText="Stuname" SortExpression="Stuname" />
<asp:BoundField DataField="Stuage" HeaderText="Stuage" SortExpression="Stuage" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
</Fields>
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:cxConnectionString %>"
SelectCommand="SELECT * FROM [student] WHERE ([StuID] = @StuID)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="StuID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>