前台:

<asp:GridView AutoGenerateColumns="false"  ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
            
<Columns>
                
<asp:BoundField DataField="Key" HeaderText="Key" />
                 
<asp:BoundField DataField="Value" HeaderText="Value" />
                 
                 
<asp:TemplateField>
                    
<ItemTemplate>
                        
<input type="button" value="显示" onclick="GoShowDetail(this,1);" />
                        
<input type="button" value="隐藏"  onclick="GoShowDetail(this,2);" />
                    
</ItemTemplate>
                 
</asp:TemplateField>
               
              
            
</Columns>
        
</asp:GridView>

<asp:Repeater ID="rp1" runat="server">
            
<HeaderTemplate>
                
<table border="1">
                
<tr>
                    
<td>类别</td>
                    
<td>时间</td>
                    
<td>金额</td>
                
</tr>
            
</HeaderTemplate>
            
<ItemTemplate>
                
<TR>
                    
<td><%#Eval("A")%></td>
                     
<td><%#Eval("B")%></td>
                     
<td><%#Eval("C")%></td>
                
</TR>
            
</ItemTemplate>
            
<FooterTemplate>
                
</TABLE>
            
</FooterTemplate>
        
</asp:Repeater>




<script type="text/javascript">
 
function GoShowDetail(btn,i)
 
{
    
var tr = btn.parentElement.parentElement.nextSibling;
    
    tr.style.display 
= (i==1)?"":"none";
 }

</script>

后台:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        
        GridViewRow row = e.Row;
        GridViewRow NewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
        NewRow.Cells.Add(new TableCell());
        NewRow.Cells[0].ColumnSpan = row.Cells.Count;
        //NewRow.Cells[0].Controls.Add(this.rp1);
        NewRow.Style.Add("display","none");

        DataTable dt = new DataTable();
        dt.Columns.Add("A", typeof(string));
        dt.Columns.Add("B", typeof(string));
        dt.Columns.Add("C", typeof(Int32));
        DataRow r1 = dt.NewRow();
        r1[0] = "软件工程";
        r1[1] = "06-01-12";
        r1[2] = 8000;
        dt.Rows.Add(r1);
        r1 = dt.NewRow();
        r1[0] = "网站开发";
        r1[1] = "06-11-10";
        r1[2] = 12000;
        dt.Rows.Add(r1);


        this.rp1.DataSource = dt.DefaultView;
        this.rp1.DataBind();



        this.GridView1.Controls[0].Controls.Add(NewRow);
        //this.rp1.RenderControl(Html32TextWriter);
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        Html32TextWriter tr = new Html32TextWriter(new System.IO.StringWriter(sb));
        this.rp1.RenderControl(tr);
        NewRow.Cells[0].Text = sb.ToString();
    }
 posted on 2007-01-12 14:29  心有灵犀  阅读(709)  评论(0编辑  收藏  举报