自CSDN)gridview例子–顯示/隱藏詳細內容

來源:互聯網
上載者:User

前台:

<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();
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.