ASP. NET DataList nested implementation Comment effect

Source: Internet
Author: User

Problem:

Datalist1 Show say data for this table

Then you want to nest a Datalist2 in Datalist1 to display the corresponding comment

Table 2 Sayid corresponds to the ID of table 1, if the corresponding Sayid is not found in table 2, Datalist2 is not displayed in the corresponding Datalist1.
The difficulty lies in how to reach the "Lin Xia" said the following only show "Huang Lei", "propelled letter" comments, "Han Han" below only show "Regulation regulation" comments. (The ID of the Lin Xia is 3, so the query corresponds to the row Sayid 3 in table 2).

Code

Front Code:

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="Server">    <meta http-equiv="Content-type" Content="text/html; Charset=utf-8 " />    <title></title>    <link href="content/bootstrap.css" rel="stylesheet" /> </head><body>    <form id="Form1" runat= "server">        <h2 style="Text-align:center;" >List of comments</H2>        <div Align="center" Style="margin-top:20px;" >            <asp:datalist ID="DataList1" runat="Server"  Onitemdatabound="Datalist1_itemdatabound" Width= "50%" CssClass=" Table table-bordered "cellpadding=" 0 " cellspacing=" 0 ">                                <ItemTemplate>                    <div>From<%# Eval("Place")%>Of<%# Eval("username")%>In<%# Eval("date")%>Published</div>                    <div>                        <%# Eval("text")%>                    </div>                    <asp:datalist ID="DataList2" runat="Server" Width= "100%">                        <ItemTemplate>                            <div Style="border:1px dashed black; margin:1px; " >                                <div>                                    <%#Eval("username")%>The comments<%#Eval("date")%>                                </div>                                <div>Comment Content:<%#Eval("text")%> </div>                            </div>                        </ItemTemplate>                    </asp:datalist>                </ItemTemplate>            </asp:datalist>        </div>    </form></body></html>

Background CS:

protected void Page_Load (object sender, EventArgs e) {if (! Page. IsPostBack) {This. DataList1. DataSource= Data ();This. DataList1. DataBind();}} public DataSet Data () {DataSet ds = new DataSet ();DataTable dtparent = new DataTable ();Dtparent. Columns. ADD("id", typeof (int));Dtparent. Columns. ADD("username", typeof (String));Dtparent. Columns. ADD("Text", typeof (String));Dtparent. Columns. ADD("Date", typeof (String));Dtparent. Columns. ADD("Place", typeof (String));Dtparent. Rows. ADD(1,"Morgan","This ...","2014.6","London");Dtparent. Rows. ADD(2,"Korean Cold","Trendy ...","2015.7","Hunan");Dtparent. Rows. ADD(3,"Lin Xia","The Years ...","2016.2","Small City");Dtparent. Rows. ADD(4,"Faye Wong","I will ...","2015.9","Hong Kong");Dtparent. Rows. ADD(5,"Guo","We ...","2015.1","Shanghai");DataTable dtchild = new DataTable ();Dtchild. Columns. ADD("id", typeof (int));Dtchild. Columns. ADD("SayId", typeof (int));Dtchild. Columns. ADD("username", typeof (String));Dtchild. Columns. ADD("Text", typeof (String));Dtchild. Columns. ADD("Date", typeof (String));Dtchild. Rows. ADD(1,3,"Huang Lei","I was the first to comment.","2016.6.17 13:06:12");Dtchild. Rows. ADD(3,3,"Propelled letter","comment on Lin Xia's saying","2016.6.17 12:26:43");Dtchild. Rows. ADD(4,2,"Regulation regulation","I comment on Han cold","2015.3.1 15:20:10");Ds. Tables. AddRange(New datatable[] {dtparent, dtchild});DataRelation dr = New DataRelation ("Relation", dtparent. Columns["id"], Dtchild. Columns["SayId"]);Ds. Relations. ADD(DR);Dtparent = ds. Relations["Relation"]. ParentTable;Dtchild = ds. Relations["Relation"]. ChildTable;Return DS;} protected void Datalist1_itemdatabound (object sender, DataListItemEventArgs e) {if (E. Item. ItemType= = ListItemType. Item|| E. Item. ItemType= = ListItemType. AlternatingItem) {DataList DataList = E. Item. FindControl("DataList2") as DataList;DataRowView ROWV = (DataRowView) E. Item. DataItem;datarow[] rows = Data (). Tables[1]. Select("Sayid= '"+ rowv["id"]. ToString() +"'");DataTable dt = Data (). Tables[1]. Clone();foreach (DataRow Drinchrows) {DT. ImportRow(DR);} dataList. DataSource= DT;DataList. DataBind();}        }

The data source portion of the background code can be replaced with the ADO query database method.

ASP. NET DataList nested implementation Comment effect

Related Article

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.