Answered by:
how to bind listview in another listview

Question
-
User836525179 posted
Hello friends, I want to call the view list into another view list. For example, we have a table that shows the list of users and we also have a table that shows the list of user posts.
I want to display the list of user posts near to the user information at the same time when the view list displays the list of usersfor example like below
userinfo1
post1
post2
userinfo2
post1
post2
post3
userinfo3
post1
post2
Monday, May 3, 2021 1:38 AM
Answers
-
User409696431 posted
I assume that when you are saying "view list", you mean "ListView". If you want to nest a ListView inside another ListView, and call for data based on values in the containing ListView's ItemTemplate, put the nested ListView and the datasource for the nested ListView inside the ItemTemplate of the containing ListView. That way each call for the nested data will find the control in the current list item to get it's query value from.
The example below is for a table with departments and department ids, and a second table of event dates associated with the department id.
In the containing ListView I fetch the department Name, and the department id I put the department id in a HiddenField. It does not display, but is available as a Control for the Select parameter of the SQLDataSource for the nested ListView. The nested ListView displays the event dates for the department, fetching them using the value of the department id in the HiddenField.
<form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <EmptyDataTemplate> <span>No departments.</span> </EmptyDataTemplate> <ItemTemplate>
<br /> Department Name: <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> <asp:HiddenField runat="server" ID="DeptID" Value='<%# Eval("Id") %>' /> <br /><br /> <asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2"> <EmptyDataTemplate> <span>No dates.</span> </EmptyDataTemplate> <ItemTemplate> Event Date: <asp:Label ID="RegisteredDateLabel" runat="server" Text='<%# Eval("RegisteredDate") %>' /> <br /> <br /> </ItemTemplate> <LayoutTemplate> <div style="" id="itemPlaceholderContainer" runat="server"> <span runat="server" id="itemPlaceholder" /> </div> <div style=""> </div> </LayoutTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [RegisteredDate] FROM [Request] WHERE ([AssignedDepartmentId] = @AssignedDepartmentId)"> <SelectParameters> <asp:ControlParameter ControlID="DeptID" Name="AssignedDepartmentId" PropertyName="Value" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </ItemTemplate> <LayoutTemplate> <div id="itemPlaceholderContainer" runat="server"> <span runat="server" id="itemPlaceholder" /> </div> <div style=""> </div> </LayoutTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Name], [Id] FROM [Department] ORDER BY [Name]"></asp:SqlDataSource> <br /> </div> </form>The result of my sample above is:
Name: Department A
Event Date: 1/7/2021 12:00:00 AM
Event Date: 1/6/2021 12:00:00 AM
Name: Department B
Event Date: 1/7/2021 12:00:00 AM
Event Date: 1/8/2021 12:00:00 AM
Event Date: 1/9/2021 12:00:00 AM
Event Date: 1/3/2021 12:00:00 AM
Event Date: 1/4/2021 12:00:00 AM- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 3, 2021 3:10 AM
All replies
-
User409696431 posted
I assume that when you are saying "view list", you mean "ListView". If you want to nest a ListView inside another ListView, and call for data based on values in the containing ListView's ItemTemplate, put the nested ListView and the datasource for the nested ListView inside the ItemTemplate of the containing ListView. That way each call for the nested data will find the control in the current list item to get it's query value from.
The example below is for a table with departments and department ids, and a second table of event dates associated with the department id.
In the containing ListView I fetch the department Name, and the department id I put the department id in a HiddenField. It does not display, but is available as a Control for the Select parameter of the SQLDataSource for the nested ListView. The nested ListView displays the event dates for the department, fetching them using the value of the department id in the HiddenField.
<form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <EmptyDataTemplate> <span>No departments.</span> </EmptyDataTemplate> <ItemTemplate>
<br /> Department Name: <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> <asp:HiddenField runat="server" ID="DeptID" Value='<%# Eval("Id") %>' /> <br /><br /> <asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2"> <EmptyDataTemplate> <span>No dates.</span> </EmptyDataTemplate> <ItemTemplate> Event Date: <asp:Label ID="RegisteredDateLabel" runat="server" Text='<%# Eval("RegisteredDate") %>' /> <br /> <br /> </ItemTemplate> <LayoutTemplate> <div style="" id="itemPlaceholderContainer" runat="server"> <span runat="server" id="itemPlaceholder" /> </div> <div style=""> </div> </LayoutTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [RegisteredDate] FROM [Request] WHERE ([AssignedDepartmentId] = @AssignedDepartmentId)"> <SelectParameters> <asp:ControlParameter ControlID="DeptID" Name="AssignedDepartmentId" PropertyName="Value" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </ItemTemplate> <LayoutTemplate> <div id="itemPlaceholderContainer" runat="server"> <span runat="server" id="itemPlaceholder" /> </div> <div style=""> </div> </LayoutTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Name], [Id] FROM [Department] ORDER BY [Name]"></asp:SqlDataSource> <br /> </div> </form>The result of my sample above is:
Name: Department A
Event Date: 1/7/2021 12:00:00 AM
Event Date: 1/6/2021 12:00:00 AM
Name: Department B
Event Date: 1/7/2021 12:00:00 AM
Event Date: 1/8/2021 12:00:00 AM
Event Date: 1/9/2021 12:00:00 AM
Event Date: 1/3/2021 12:00:00 AM
Event Date: 1/4/2021 12:00:00 AM- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 3, 2021 3:10 AM -
User1535942433 posted
Hi uniservice3,
What's your codes? What's your problem you have meet? If you need an example,you could refer to below this:
<asp:ListView ID="mainMenu" runat="server" DataKeyNames="enter column name you want"> <ItemTemplate> <li><asp:HyperLink ID="mainLinks" runat="server" NavigateUrl='<%# Eval("name", "~/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink> <ul class="super-child"> <asp:ListView ID="childMenu" runat="server"> <ItemTemplate> <li><asp:HyperLink ID="cat3" runat="server" NavigateUrl='<%# Eval("category") & Eval("name", "/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink></li> </ItemTemplate> </asp:ListView> </ul> </li> </ItemTemplate> </asp:ListView>
Code-behind:
protected void onItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { ListViewDataItem itm = (ListViewDataItem)e.Item; string name = mainMenu.DataKeys(itm.DataItemIndex)("enter your datakeyname"); ListView childMenu = e.Item.FindControl("childMenu") as ListView; string constr = ConfigurationManager.ConnectionStrings("conio").ConnectionString; using (MySqlConnection con = new MySqlConnection(constr)) { using (MySqlCommand cmd = new MySqlCommand()) { cmd.CommandText = "SELECT * FROM tablename WHERE column = '" + name + "'"; cmd.Connection = con; using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); childMenu.DataSource = dt; childMenu.DataBind(); } } } } }
Best regards,
Yijing Sun
Monday, May 3, 2021 7:47 AM -
User-1716253493 posted
I assume you have listview2 in listview1 itemtemplate
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { if(e.Item.ItemType==ListViewItemType.DataItem) { string userid = string.Format("{0}", DataBinder.Eval(e.Item.DataItem, "userid")); ListView childlistview = (ListView)e.Item.FindControl("ListView2"); } }
In the code above, you get ListView2 and userid value from ListView1 item
Populate posts ListView2 with datasource filtered/query by userid above
Monday, May 3, 2021 5:39 PM