Self-CSDN) gridview example-show/hide details

Source: Internet
Author: User

Front-end:

<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 = "display" onclick = "GoShowDetail (this, 1);"/>
<Input type = "button" value = "hide" onclick = "GoShowDetail (this, 2);"/>
</ItemTemplate>
</Asp: TemplateField>


</Columns>
</Asp: GridView>

<Asp: Repeater ID = "rp1" runat = "server">
<HeaderTemplate>
<Table border = "1">
<Tr>
<Td> Category </td>
<Td> time </td>
<Td> amount </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>

Background:

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] = "Software Engineering ";
R1 [1] = "06-01-12 ";
R1 [2] = 8000;
Dt. Rows. Add (r1 );
R1 = dt. NewRow ();
R1 [0] = "website development ";
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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.