Nested asp.NET ListView - asp.net

I have a structure that looks like this:
Strategies 1..n Objectives
Objective 1..n Initiatives
Each are in a different table and linked through foreign keys.
I want to be able to nest my results in a few list view (3 to be exact).
When I try to create a dynamic ID for my nested ListView, the code doesn't compile anymore.
<asp:ListView ID="ObjectivesListView" runat="server">
<LayoutTemplate>
<table style="width:100%">
<tr style="align-content:flex-start">
<th>#</th>
...
</tr>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr >
<td onclick='toggleDiv("obj",<%#Eval("ID") %>)'>
<%# Container.DataItemIndex + 1 %>
</td>
...
</tr>
<tr id='obj<%#Eval("ID") %>' class="hide">
<td colspan="7">Another item
<!-- New list view with the initiative for Objective x -->
<asp:ListView runat="server" ID="initiativeListView">
<LayoutTemplate></LayoutTemplate>
<ItemTemplate></ItemTemplate>
</asp:ListView>
<!-- New list view with the initiative for Objective x -->
</td>
</tr>
</ItemTemplate>
</asp:ListView>
I have tried to create a dynamic ID for my second listView by doing ID="initiativeListView<%#Eval(ID)%>"> and that causes the error.
I am also running in a sharepoint environment and limited to the ASP classes. I wanted to preload and display it when the user clicks on the row.
Any Ideas?

In your child control instead of Eval(ID) you need to use
Eval(Container.Parent, "DataItem.ID")%
in first child and
Eval(Container.Parent.Parent, "DataItem.ID")%
in the second child of your nested controls

Related

ASP Nested Repeater IDs

I'm using bootstrap to collapse and expand a table, which is working fine but I'm using classes instead of IDs. With this, expanding one row expands all the rows rather than just that one. My question is how does my data-target point at a nested repeater id? The transactionCollapse ID is unable to be targeted directly and I've tried doing <%=transactionGroupedList.FindControl("transactionCollapse")%> but it threw an error.
<tbody>
<asp:Repeater runat="server" ID="transactionGroupedList" OnItemDataBound="TransactionGroupedDataList_ItemDataBound">
<ItemTemplate>
<tr>
<!-- This line should target the transactionCollapse ID below instead of the class -->
<td data-toggle="collapse" data-target=".transactionCollapse">
<span id="transactionGroupCollapseIcon" runat="server" class="fonticon-down-arrow"></span>
<custom:Label runat="server" ID="transactionActivityDataColumnLabel"></custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionDateDataColumnLabel">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionNumberDataColumnLabel">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionAmountDataColumnLabel">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionStatusDataColumnLabel">
</custom:Label>
</td>
</tr>
<asp:Repeater runat="server" ID="transactionDetailList" OnItemDataBound="TransactionDetailsDataList_ItemDataBound">
<ItemTemplate>
<tr id="transactionCollapse" runat="server" class="collapse transactionCollapse">
<td colspan="2">
<custom:Label runat="server" ID="transactionDetail">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionDetailTransactionNumber">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionDetailAmount">
</custom:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</tbody>
The Online Payment row is what collapses/expands the Posting - and MP Payment rows below. This user only has one Online Payment, but many users will have multiple.
You have a couple of problems. First of all when using FindControl inside a Repeater/GridView etc is index based. So you need to use FindControl on the correct Item.
transactionGroupedList[i].FindControl("transactionCollapse")
However the above will still not work because transactionCollapse is in a nested Repeater that needs to be found first and then access the correct Item Index.
transactionGroupedList.Items[0].FindControl("transactionDetailList").Items[0]...
But this will also not work since FindControl does not know that transactionDetailList is a Repeater with index based Items. So you need to cast the nested Repeater first before you can access it's items. So it becomes this
<%= ((Repeater)transactionGroupedList.Items[i].FindControl("transactionDetailList")).Items[i].FindControl("transactionCollapse").ClientID %>

Foreign key table field not displaying within a standalone dynamic data enabled list view

I've gone through hundreds of Google result pages, trying to find an answer for this over the past couple of days.
Using a standalone dynamic data page to display a table, using an EntityDataSource, with one of the fields being a foreign key to a sub-table. I want it to display the value from the sub-table. I've been playing with a simplified case using the NorthWinds database (see code below). If I assign the DynamicControl a DataField="Supplier", it displays the sub-table/class name ("DAL.Supplier") instead. If I try assigning DataField="Supplier.CompanyName", it errors with "The table 'Product' does not have a column named 'Supplier.CompanyName'". Since I want to take advantage of dynamic data's editing features, using a templated field with <%# Eval("Supplier.CompanyName") %> is out.
Is this just not possible with a stand-alone dynamic data page? It clearly works fine within a fully scaffolded system. Or am I (hopefully) just missing something? Thank you.
Test.aspx
<%# Page Title="" Language="VB" MasterPageFile="~/Master/Site.master" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" %>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContent" Runat="Server">
<asp:ListView ID="ListView1" runat="server" DataSourceID="theDataSource" DataKeyNames="ProductID">
<ItemTemplate>
<tr>
<td>
<asp:DynamicControl runat="server" DataField="ProductID" Mode="Edit" />
</td>
<td>
<asp:DynamicControl runat="server" DataField="ProductName" Mode="Edit" />
</td>
<td>
<asp:DynamicControl runat="server" DataField="Supplier" Mode="Edit" />
</td>
<td>
<asp:DynamicControl runat="server" DataField="UnitPrice" Mode="Edit" />
</td>
<td>
<asp:DynamicControl runat="server" DataField="Discontinued" Mode="Edit" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0">
<tr runat="server">
<th runat="server">
ProductID
</th>
<th runat="server">
ProductName
</th>
<th runat="server">
Supplier
</th>
<th runat="server">
UnitPrice
</th>
<th runat="server">
Discontinued
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:EntityDataSource ID="theDataSource" runat="server"
ConnectionString="name=NorthwindEntities"
DefaultContainerName="NorthwindEntities" EnableDelete="True"
EnableFlattening="False" EnableInsert="True" EnableUpdate="True"
Include="Supplier"
EntitySetName="Products">
</asp:EntityDataSource>
</asp:Content>
Test.aspx.vb
Imports System.Web.DynamicData
Imports DAL
Partial Class Test
Inherits System.Web.UI.Page
Protected table As MetaTable
Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
Listview1.EnableDynamicData(GetType(Product))
table = theDataSource.GetTable()
Title = table.DisplayName
End Sub
End Class
Problem solved. When all of this had started, I had originally gotten the error message:
Could not determine a MetaTable. A MetaTable could not be determined for the data source 'EntityDataSource1' and one could not be inferred from the request URL. Make sure that the table is mapped to the dats source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute
Trying to track that down led me to getting rid of the asp:DynamicDataManager markup, and replacing it in the code-behind with:
Listview1.EnableDynamicData(GetType(Product))
table = theDataSource.GetTable()
That turns out to have led me down the proverbial rabbit hole... While it got rid of the MetaTable error, it inadvertently caused the problem I wrote about above. Many thanks to: http://daviworld.net/?tag=/DynamicDataManager who points out that this error message is actually caused by:
This is due to an issue with the Designer code generation. When you select a DataSource for the GridView control, it does not add the ContextTypeName Attribute to the EntityDataSourceControl.
Once I removed the lines of code above, and added back in the asp:DynamicDataManager to the markup, and then added a ContextTypeName="DAL.NorthwindEntities" to the EntitiyDataSource, everything now works as one would wish it to.
Yeah true what u said. im at the same place where u got stuck. am running from pillar to post with fire in my belly but i guess it might extinguish soon.
how can it be so screwed up that u cannot work outside the scaffolding mechanism, all u are really doing is borrowing a dynamicdatamanager so u can stay templatised across both their framework and your newly added framework.
the way i got into this hole is when i try to display multiple entities data on the same page. thumb of rule seems to be single entity , single page. whatever im on da error for now. give it another few days before i open up the bottle again.. let there be beer !!!

Counting ItemTemplate in ListView from XmlDataSource in ASP.net

I have the following code. It works as is, but... I am not always going to have an even number of items in the RSS feed So, at the end of the table, I might have only one table cell on the last row. So, is there a way to count the number of ItemTemplates and AlternatingItemTemplate, so if it is an odd number I would be able to add another cell <td> </td></tr> and close the table row?
<asp:XmlDataSource ID="SomeFeed" DataFile="TestSomeRSS.xml" XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:ListView ID="SomeFeedScroller" DataSourceID="SomeFeed" ItemPlaceholderID="SomePlcID" runat="server">
<LayoutTemplate>
<table id="ListingsTable" cellpadding="0" cellspacing="0" align="center">
<asp:PlaceHolder ID="SomePlcID" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="vertical-align:top;">
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
Thanks in advance for your help.
I'm not sure what you're asking, but why not just put a complete row in the ItemTemplate and AlternatingItemTemplate, like this:
<ItemTemplate>
<tr style="vertical-align:top;">
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="vertical-align:top;">
<td class="bnotes" style="width:325px;padding:5px;">
<%# XPath("title")%><br />
<%# XPath("description")%><br />
</td>
</tr>
</AlternatingItemTemplate>
That way you don't have to figure it out yourself - just let the control render itself.
EDITED TO ADD
Looking at your posted code again, it looks like you might have been attempting one row of alternating cell styles. I think you misunderstood the intent of the ItemTemplate and AlternatingItemTemplates; they usually deal with the fields (columns) of a given record.
In this case, you'd have the first RSS feed item in an ItemTemplate, then the second RSS feed item in an AlternateItemTemplate (i.e., another row), then the third RSS feed item in an ItemTemplate and so on.
I hope this helps - if I've misunderstood what you're trying to do let me know.
2nd Edit
Based on the example layout posted in the comments, I think the DataList Class would be a better option, as you can easily specify multiple columns (using the RepeatColumns property). Something like this:
<asp:XmlDataSource ID="SomeFeed" DataFile="TestSomeRSS.xml" XPath="rss/channel/item" runat="server">
</asp:XmlDataSource>
<asp:DataList ID="SomeFeedScroller" DataSourceID="SomeFeed"
RepeatColumns="2" RepeatDirection="Horizontal"
RepeatLayout="Table" runat="server">
<ItemStyle CssClass="bnotes" Vertical-Align="top" Width="325px" />
<AlternatingItemStyle CssClass="bnotes" vertical-Align="top" Width="325px" />
<ItemTemplate>
<%# XPath("title")%><br />
<%# XPath("description")%>
</ItemTemplate>
<AlternatingItemTemplate>
<%# XPath("title")%><br />
<%# XPath("description")%>
</AlternatingItemTemplate>
</asp:DataList>
The above is not tested, but the general idea was to keep the formatting as close to what was in the ListView as possible.
Another possible approach might be something similar to this thread on having multiple columns in a Repeater control: Multiple columns in a repeater.
The DataList control supports editing, selecting, updating, etc like ListView. The Repeater control does not.

difference between item template and layout template

what is the difference between item template and the layout template. in layout template only we have information about the designing? or any thing else. i am unable to understand the item template.. please explain..!
In addition to this one i have query in project like this
SELECT TOP (1) ProductName, UnitPrice FROM Products ORDER BY NEWID()
here NEWID() means what? is it predefined function related to sqlserver? there is no any newid() function in my project which was downloaded. if it is predefined function then what it can do?
Thank you
The main layout of a ListView control is created by defining a LayoutTemplate. The LayoutTemplate will include controls that acts as a placeholder for the data like Table, Panel, Label or HTML controls like table, div, or span elements that have a runat attribute set to "server".
Item template is the main template which will show the data bounded to the ListView in a repeated manner. This template typically contains controls that are data-bound to data columns or other individual data elements. These two templates are mandatory.
GroupTemplate will be used to group the items. The EditItemtemplate, SelectedItemTemplate, InsertItemTemplate are shown at that particular operation like insert, edit, select. ItemSeparatorTemplate, GroupSeparatorTemplate are used to separate the individual items and group Items Separately.
Here this makes difference ItemPlaceholderID="itemPlaceholder"
<asp:ListView runat="server" ID="ListView1" ItemPlaceholderID="itemPlaceholder">
<LayoutTemplate>
<table border="0" cellpadding="1">
<tr style="background-color:#E5E5FE">
<th align="left"><asp:LinkButton ID="lnkId" runat="server">Id</asp:LinkButton></th>
<th align="left"><asp:LinkButton ID="lnkName" runat="server">Name</asp:LinkButton></th>
<th align="left"><asp:LinkButton ID="lnkType" runat="server">Type</asp:LinkButton></th>
<th></th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblName"><%#Eval("FirstName")+"
"+Eval("LastName") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblType"><%#Eval("Type") %></asp:Label></td>
<td></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#EFEFEF">
<td><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblName"><%#Eval("FirstName")+" "+
Eval("LastName") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblType"><%#Eval("Type") %></asp:Label></td>
<td></td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
Reference links: reference site, code project reference
It looks like you're using the ListView control.
The ItemTemplate property only applies to the data item bound to the control. The LayoutTempate allows you to define the layout for everything else.
Let's say your wanted to render your data using a . your LayoutTemplate would contain your table definition with a single, empty row with ID "itemPlaceHolder"
<tr id="itemPlaceHolder" runat="server" />
Your item template would then define how your s should be rendered.

Span columns in datagrid

I have a datagrid where some of the text needs to span multiple columns. Here is an example of what I need.
Row # Image Name Price Date
1 xxx My Name $99 1/1/2009
xxx
xxx Long description goes here
2 xxx name 2 $99 1/1/2009
xxx
xxx Another long description
Is something like this possible in Asp.Net using a datagrid? Any suggestions on how to do this?
You might find it easier to use an ASP.Net Repeater instead, and have multiple rows per DataItem. That way, you get complete control over the layout.
It's technically possible to use the ASP.Net GridView to do it, but I suspect this wouldn't be the most elegant solution here. The GridView (and Datagrid) were designed out of the box for one row per DataItem.
Here is some sample ASP.Net containing a Repeater that illustrates an approach that might work for you:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<tr>
<th>
Col 1
</th>
<th>
Col 2
</th>
<th>
Col 3
</th>
</tr>
<ItemTemplate>
<tr>
<td>
<%# Eval("Field1") %>
</td>
<td>
<%# Eval("Field2") %>
</td>
<td>
<%# Eval("Field3") %>
</td>
</tr>
<tr>
<td>
<%# Eval("Field4") %>
</td>
<td colspan="2">
<%# Eval("Field5") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Note that the ItemTemplate contains two HTML table rows, the second of which contains a td with a colspan of 2.
Hope that helps a little.
Do you have to use a DataGrid?, I would suggest the ListView control. It has all the functionality you need and uses templates for full UI control.

Resources