Answered by:
How do i display Datalist B inside Datalist B, Datalist A from Post Table while Datalist B from B table

Question
-
User-2074858223 posted
Hi i have Datalist A which displays the Post in table A but then i want to display user comments of Datalist B inside Datalist A so that each post shows the comments of every posts.
Html
<asp:DataList ID="GetUSERPOSTS" runat="server"> <ItemTemplate> <asp:Label ID="lblshId" Text='<%#Eval("UserName") %>' runat="server" /> <asp:Label ID="lblname" Text='<%#Eval("Name") %>' runat="server" /> <asp:Label ID="lbpost" Text='<%#Eval("Posts") %>' runat="server" /> <asp:DataList ID="GetUSERCOMMENTS2" runat="server"> <ItemTemplate> <asp:Label ID="lblshId2" Text='<%#Eval("UserName") %>' runat="server" /> <asp:Label ID="lbls" Text='<%#Eval("Name") %>' runat="server" /> <asp:Label ID="lbcomments" Text='<%#Eval("Comments") %>' runat="server" /> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList>
//User Comments below
if (!this.IsPostBack) { this.GetPOSTCOMMENTS(); } } public int ID; private void GetPOSTCOMMENTS() { username = this.Page.User.Identity.Name; //string userId = Session["UserId"].ToString(); string getADPOST = "GetUSERcomment"; using (SqlConnection con = new SqlConnection(constring)) { con.Open(); using (SqlCommand cmd = new SqlCommand(getADPOST, con)) { cmd.CommandType = CommandType.StoredProcedure; { cmd.Parameters.AddWithValue("@UserName", Session["userName"]); cmd.Parameters.AddWithValue("@FriendUserName", username); cmd.Parameters.AddWithValue("@ID", Session["userName"]); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); GetUSERCOMMENTS.DataSource = dt; GetUSERCOMMENTS.DataBind(); } }// User Posts below
if (!this.IsPostBack) { this.Populatebooks(); string username5 = this.Page.User.Identity.Name; //Path.GetFileName(Request.Url.AbsolutePath); } } private void Populatebooks() { // string userId = Session["userName"].ToString(); // string userId = Session["userName"].ToString(); // string str = ConfigurationManager.ConnectionStrings["DB"].ConnectionString; string Populatebooks = "GetUserPOSTS"; using (SqlConnection con = new SqlConnection(constring4)) { con.Open(); using (SqlCommand cmd = new SqlCommand(Populatebooks, con)) { cmd.CommandType = CommandType.StoredProcedure; // cmd.Parameters.AddWithValue("@Name", Request.QueryString["Id"].ToString()); cmd.Parameters.AddWithValue("@UserName", Session["userName"]); // cmd.Parameters.AddWithValue("@Id", Id); //lblpost.Text = TextBixcomment.Text.Replace(Environment.NewLine, "<BR />"); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); ViewState["DataTable"] = dt; GetUserPosts.DataSource = dt; GetUserPosts.DataBind(); } }Saturday, November 25, 2017 6:00 PM
Answers
-
User1400794712 posted
Hi micah2012,
My solution can show both Comments and Posts, you can have a try. I don't have your stored procedure, so I set value to posts and comments manually. Here is my full demo:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Populatebooks(); } } public int ID; private void Populatebooks() { //Set value to POSTS DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[3] { new DataColumn("UserName"), new DataColumn("Name"), new DataColumn("Posts") }); dt1.Rows.Add("A", "aa", "aaaaaaa"); dt1.Rows.Add("B", "bb", "bbbbbbb"); dt1.Rows.Add("C", "cc", "ccccccc"); dt1.Rows.Add("D", "dd", "ddddddd"); dt1.Rows.Add("E", "ee", "eeeeeee"); GetUSERPOSTS.DataSource = dt1; GetUSERPOSTS.DataBind(); } //The following method replaces with GetPOSTCOMMENTS method. protected void GetUSERPOSTS_ItemDataBound(object sender, DataListItemEventArgs e) { //Set value to comments of each post DataRowView drv = e.Item.DataItem as DataRowView; DataList GetUSERCOMMENTS = e.Item.FindControl("GetUSERCOMMENTS") as DataList; DataTable dt2 = new DataTable(); dt2.Columns.AddRange(new DataColumn[3] { new DataColumn("UserName"), new DataColumn("Name"), new DataColumn("Comments") }); dt2.Rows.Add("A", "aa", "1111111"); dt2.Rows.Add("B", "bb", "2222222"); dt2.Rows.Add("C", "cc", "3333333"); dt2.Rows.Add("D", "dd", "4444444"); dt2.Rows.Add("E", "ee", "5555555"); GetUSERCOMMENTS.DataSource = dt2; GetUSERCOMMENTS.DataBind(); }
<body> <form id="form1" runat="server"> <div> <asp:DataList ID="GetUSERPOSTS" runat="server" OnItemDataBound="GetUSERPOSTS_ItemDataBound"> <ItemTemplate> <asp:Label ID="lblshId" Text='<%#Eval("UserName") %>' runat="server" /> <asp:Label ID="lblname" Text='<%#Eval("Name") %>' runat="server" /> <asp:Label ID="lbpost" Text='<%#Eval("Posts") %>' runat="server" /> <asp:DataList ID="GetUSERCOMMENTS" runat="server"> <ItemTemplate> <asp:Label ID="lblshId2" Text='<%#Eval("UserName") %>' runat="server" /> <asp:Label ID="lbls" Text='<%#Eval("Name") %>' runat="server" /> <asp:Label ID="lbcomments" Text='<%#Eval("Comments") %>' runat="server" /> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> </div> </form> </body>
How it works:
Best Regards,
Daisy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 28, 2017 2:57 AM
All replies
-
User1400794712 posted
Hi micah2012,
When we set datalist inside datalist, we can't get the Sub-datalist's ID in C# directly. You can set value to the sub-datalist in parent datalist's OnItemDataBound event:
1.Add OnItemDataBound event for Post Datalist:
<asp:DataList ID="GetUSERPOSTS" runat="server" OnItemDataBound="GetUSERPOSTS_ItemDataBound"> </asp:DataList>
2.Set value to sub-datalist in OnItemDataBound method:
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { Populatebooks(); } } private void Populatebooks() { //No changes. } //Remove GetPOSTCOMMENT() methods and set value to GetUSERCOMMENTS in following method. protected void GetUSERPOSTS_ItemDataBound(object sender, DataListItemEventArgs e) { username = this.Page.User.Identity.Name; //string userId = Session["UserId"].ToString(); string getADPOST = "GetUSERcomment"; using (SqlConnection con = new SqlConnection(constring)) { con.Open(); using (SqlCommand cmd = new SqlCommand(getADPOST, con)) { cmd.CommandType = CommandType.StoredProcedure; { cmd.Parameters.AddWithValue("@UserName", Session["userName"]); cmd.Parameters.AddWithValue("@FriendUserName", username); cmd.Parameters.AddWithValue("@ID", Session["userName"]); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); GetUSERCOMMENTS.DataSource = dt; GetUSERCOMMENTS.DataBind(); } } }
Best Regards,
DaisyMonday, November 27, 2017 10:11 AM -
User-2074858223 posted
Your solutions looks like only GetUSERcomment will be showing on GetUSERCOMMENTS, mean while the user post is not showing
GetUSERPOSTS
Monday, November 27, 2017 1:27 PM -
User1400794712 posted
Hi micah2012,
My solution can show both Comments and Posts, you can have a try. I don't have your stored procedure, so I set value to posts and comments manually. Here is my full demo:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Populatebooks(); } } public int ID; private void Populatebooks() { //Set value to POSTS DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[3] { new DataColumn("UserName"), new DataColumn("Name"), new DataColumn("Posts") }); dt1.Rows.Add("A", "aa", "aaaaaaa"); dt1.Rows.Add("B", "bb", "bbbbbbb"); dt1.Rows.Add("C", "cc", "ccccccc"); dt1.Rows.Add("D", "dd", "ddddddd"); dt1.Rows.Add("E", "ee", "eeeeeee"); GetUSERPOSTS.DataSource = dt1; GetUSERPOSTS.DataBind(); } //The following method replaces with GetPOSTCOMMENTS method. protected void GetUSERPOSTS_ItemDataBound(object sender, DataListItemEventArgs e) { //Set value to comments of each post DataRowView drv = e.Item.DataItem as DataRowView; DataList GetUSERCOMMENTS = e.Item.FindControl("GetUSERCOMMENTS") as DataList; DataTable dt2 = new DataTable(); dt2.Columns.AddRange(new DataColumn[3] { new DataColumn("UserName"), new DataColumn("Name"), new DataColumn("Comments") }); dt2.Rows.Add("A", "aa", "1111111"); dt2.Rows.Add("B", "bb", "2222222"); dt2.Rows.Add("C", "cc", "3333333"); dt2.Rows.Add("D", "dd", "4444444"); dt2.Rows.Add("E", "ee", "5555555"); GetUSERCOMMENTS.DataSource = dt2; GetUSERCOMMENTS.DataBind(); }
<body> <form id="form1" runat="server"> <div> <asp:DataList ID="GetUSERPOSTS" runat="server" OnItemDataBound="GetUSERPOSTS_ItemDataBound"> <ItemTemplate> <asp:Label ID="lblshId" Text='<%#Eval("UserName") %>' runat="server" /> <asp:Label ID="lblname" Text='<%#Eval("Name") %>' runat="server" /> <asp:Label ID="lbpost" Text='<%#Eval("Posts") %>' runat="server" /> <asp:DataList ID="GetUSERCOMMENTS" runat="server"> <ItemTemplate> <asp:Label ID="lblshId2" Text='<%#Eval("UserName") %>' runat="server" /> <asp:Label ID="lbls" Text='<%#Eval("Name") %>' runat="server" /> <asp:Label ID="lbcomments" Text='<%#Eval("Comments") %>' runat="server" /> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> </div> </form> </body>
How it works:
Best Regards,
Daisy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 28, 2017 2:57 AM