选择GridView的一行,用DetailsView显示详细内容


源码位置: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显示详细内容了。

 

 

[csharp]  view plain copy
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.   
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>无标题页</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.  <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"  
  12.    
  13.             BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2"  
  14.    
  15.             DataKeyNames="StuID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None"  
  16.    
  17.             PageSize="5" AutoGenerateSelectButton="True">  
  18.    
  19.             <FooterStyle BackColor="Tan" />  
  20.    
  21.             <Columns>  
  22.    
  23.                 <asp:BoundField DataField="StuID" HeaderText="StuID" SortExpression="StuID"   
  24.                     InsertVisible="False" ReadOnly="True" />  
  25.    
  26.                 <asp:BoundField DataField="Stuname" HeaderText="Stuname" SortExpression="Stuname" />  
  27.    
  28.                 <asp:BoundField DataField="Stuage" HeaderText="Stuage" SortExpression="Stuage" />  
  29.    
  30.                 <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />  
  31.    
  32.             </Columns>  
  33.    
  34.             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />  
  35.    
  36.             <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />  
  37.    
  38.             <HeaderStyle BackColor="Tan" Font-Bold="True" />  
  39.    
  40.             <AlternatingRowStyle BackColor="PaleGoldenrod" />  
  41.    
  42.         </asp:GridView>  
  43.    
  44.         <br />  
  45.    
  46.         数据行详细信息:<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cxConnectionString %>"  
  47.    
  48.               
  49.             SelectCommand="SELECT [StuID], [Stuname], [Stuage], [Grade] FROM [student]"></asp:SqlDataSource>  
  50.    
  51.         <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White"  
  52.    
  53.             BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource2"  
  54.    
  55.             GridLines="Vertical" Height="50px" Width="325px">  
  56.    
  57.             <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  58.    
  59.             <EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  60.    
  61.             <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
  62.    
  63.             <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  64.    
  65.             <Fields>  
  66.    
  67.                 <asp:BoundField DataField="StuID" HeaderText="StuID" SortExpression="StuID">  
  68.    
  69.                     <ControlStyle Width="80px" />  
  70.    
  71.                 </asp:BoundField>  
  72.    
  73.                 <asp:BoundField DataField="Stuname" HeaderText="Stuname" SortExpression="Stuname" />  
  74.    
  75.                 <asp:BoundField DataField="Stuage" HeaderText="Stuage" SortExpression="Stuage" />  
  76.    
  77.                 <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />  
  78.    
  79.                 <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />  
  80.    
  81.                 <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />  
  82.    
  83.                 <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />  
  84.    
  85.             </Fields>  
  86.    
  87.             <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
  88.    
  89.             <AlternatingRowStyle BackColor="#DCDCDC" />  
  90.    
  91.         </asp:DetailsView>  
  92.    
  93.         <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:cxConnectionString %>"  
  94.    
  95.             SelectCommand="SELECT * FROM [student] WHERE ([StuID] = @StuID)">  
  96.    
  97.             <SelectParameters>  
  98.    
  99.                 <asp:ControlParameter ControlID="GridView1" Name="StuID" PropertyName="SelectedValue"  
  100.    
  101.                     Type="Int32" />  
  102.    
  103.             </SelectParameters>  
  104.    
  105.         </asp:SqlDataSource>  
  106.    
  107.    
  108.    
  109.     </div>  
  110.   
  111.     </form>  
  112. </body>  
  113. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值