I Use modelbinding to pass a list of Post Objects containing post id,Title.. to my page.aspx
Each Post Objects also contains a list of Comment Objects and these are
the ones i want to access in the inner repeater.
Here the code from my page.aspx
<asp:Repeater runat="server" ID="repeater1" ItemType="OOPBLOG.Model.Classes.Post"
SelectMethod="LoadPosts"
OnCallingDataMethods="postView">
<ItemTemplate >
<table>
<tr>
<td><%#Item.Title %></td>
</tr>
<tr>
<td><%#Item.Body %></td>
</tr>
<tr>
<td><%#Item.Date %></td>
</tr>
<asp:Repeater runat="server" ID="repeater2">
<ItemTemplate>
<table>
<tr>
<td>
<%# ((RepeaterItem)Container.Parent.Parent).ItemType %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</table>
</ItemTemplate>
</asp:Repeater>
To conclude, what i want, is to access the Item.Comments List in the nested repeater. How can i do this?
Related
Background: My data model is a List<PersonDetail>, where PersonDetail contains properties like FirstName, LastName, etc, and the List can be any size.
I want to create a table that can compare these PersonDetails side-by-side. This means for each column I need to take the data in a single PersonDetail and spread it across my 8 rows. Then do the same for the next PersonDetail.
I have a constraint where I can only use an Asp.Net control (ListView, GridView, Repeater, etc.) along with control.DataSource and control.DataBind() to create my table.
The template I want to do is something like:
<LayoutTemplate>
<table>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder2"></asp:PlaceHolder>
</tr>
<!-- and so on -->
</table>
</LayoutTemplate>
<ItemTemplate1>
<td>
<asp:Label ID="LabelFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName")%>'></asp:Label>
</td>
</ItemTemplate1>
<ItemTemplate2>
<td>
<asp:Label ID="LabelLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LastName")%>'></asp:Label>
</td>
</ItemTemplate2>
<!-- and so on -->
Where the above ItemTemplates would be the only parts that repeat in the table.
I've tried all three control options and it doesn't seem to be supported without complicating the code-behind.
Is there a better data model I could use that can interface with another control? I've seen solutions like DataTable, but not sure if that will solve my problem..
Any ideas?
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "FirstName") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "LastName") %></td>
</tr>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "Street") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "City") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
Below is code that I'm aiming for. I need a custom user control that can be data bound to, but also contain other content (so just a raw repeater wont suffice). The end goal is something along the lines of:
<MyControls:Control1 runat="server" id="Control1">
<headertemplate>
<tr>
<td>ID</td>
<td>Username</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%#((User)Container.DataItem).ID %></td>
<td><%#((User)Container.DataItem).Username %></td>
</tr>
</itemtemplate>
</MyControls>
And:
var users = GetUsersList();
Control1.DataSource = users;
Control1.DataBind();
And looks like this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl1.ascx.cs" Inherits="Controls.MyControl1" %>
<asp:PlaceHolder runat="server" ID="Wrapper">
<h2>Results</h2>
<table>
<%=HeaderTemplate%>
<%
if(ItemTemplate.AnyItems()){
foreach(var item in ItemTemplate){
}
}
else
{
%>Nothing here<%
}
%>
</table>
<MyControls:AnotherControl runat="server" />
</asp:PlaceHolder>
I've found a page on ScottGu's Blog that appears to show what I want:
https://weblogs.asp.net/scottgu/Supporting-Templates-with-ASP.NET-User-Controls
But the linked to tutorial 404's now! All other examples I've found don't seem to be well written and very hard to pick apart.
Any help on how to achieve the above would be much appreciated.
It looks like you are getting 2 different Controls mixed up. UserControl and Repeater (I think).
To bind data in to a Control inside a UserControl, you need to make that Control accessible from the parent. You can do this by creating a public property inside the UserControl.
public Repeater myRepeater
{
get
{
return Repeater1;
}
set
{
Repeater1 = value;
}
}
UserControl ascx with the Repeater Control
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr>
<td>ID</td>
<td>Username</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ID") %></td>
<td><%# Eval("Username") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Now you have access to Repeater1 in the parent due to the public property.
Control1.myRepeater.DataSource = users;
Control1.myRepeater.DataBind();
And leave the control on the aspx parent empty.
<MyControls:Control1 runat="server" id="Control1"></MyControls>
this is my code to view products as list view inside each item in list view i want to view list of items how can i do this? i can not understand the concept of the eval function and i want to know if i can pass to it list items from code behind without data Bind or it basically depends on data-bind ?
<asp:ListView ID="mylistView" runat="server" GroupItemCount="3">
<EmptyDataTemplate>
<table id="Table2" runat="server">
<tr>
<td>
No data was returned.
</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td id="Td3" runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
<ItemTemplate>
<table border="0" width="300" style="display: inline-block; background-color: Lime;">
<tr>
<td>
 
</td>
<td>
<a href='ContactUs.aspx'>
<image src='productsImg/<%# Eval("Pic_Path") %>' width="100" height="75" border="0">
</a> 
</td>
<td>
<a><b style="text-decoration: underline;">
<%# Eval("MenuName") %></b> </a>
<br />
want to view here list of items
I
</td>
</tr>
</table>
</ItemTemplate>
<LayoutTemplate>
<table id="Table1" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="groupPlaceholderContainer" runat="server">
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td2" runat="server">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
this is code behind...
dataSource.ConnectionString = connetionString;
dataSource.SelectCommand = "SELECT * FROM tbl_WebMenu where MenuID like'3_';";
mylistView.DataSource = dataSource;
mylistView.DataBind();
thanks in advance.
Eval is used to bind to an UI control that is supposedly read only. it can be used to set any type of property of control.
The complete syntax is "Databinder.Eval", it has to be used in conjunction with databind.
Look at this example.
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblRead" runat="server" Text='<%# Eval("FieldName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
you can even set a property of control using eval. it provides flexibility.
But there is a downside of eval as it uses reflection to evaluate the expression.
Read more here http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx#remarksToggle
Ok so I have an update panel sorrounding my controls. I have 2 dropdownlists which have functions they run from codebehind and a repeater of items. I've done the test in a Repeater using a button it doesn't do a postback, but the linkbutton does. What am I doing wrong?
also this is inside a usercontrol no aspx page.
<asp:UpdatePanel ID="upLocation" runat="server" UpdateMode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:Repeater ID="rptMuniProducts" runat="server">
<HeaderTemplate>
<table class="table">
<thead>
<tr>
<th class="w80"></th>
<th>Product</th>
<th>Product Type</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="actions">
<asp:Button ID="btnProd" runat="server" OnClick="btnProd_Click" Text="test" />
<asp:LinkButton ID="lnkDeleteProd" runat="server" OnClick="lnkDeleteProd_Click">Link Test</asp:LinkButton>
<asp:HiddenField ID="hdnId" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.Id") %>' />
</td>
<td><%# DataBinder.Eval(Container, "DataItem.Name") %></td>
<td><%# DataBinder.Eval(Container, "DataItem.Producttype") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Have you tried setting ClientIDMode=Auto on the LinkButton? There's a long-standing .NET bug with doPostBack and non-auto client ids.
I am populating a repeater on my asp.net web page (pop up) from a stored procedure call. I have a column where I need to build the hyperlink based on the values dynamically that calls an inquiry back to my original web form. However, the trouble I am having is that when I click on the link, I need to also retrieve some other data and insert this data into the viewstate.
The "GetListOfValues" function that I am calling obviously doesn't work the way I have this coded. Is there a way to accomplish this, and maybe even do it better than I am attempting to do it?
Here's my example:
Default.aspx
<asp:Repeater ID="rptReport" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>Name</td>
<td>ID</td>
<td>Value1</td>
<td>Value2</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"Name") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"ID") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"Value1") %></td>
<td><%# GetValueTwoLink(Eval("Name"),Eval("Value2") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<%--This area holds totals for columns--%>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
Default.aspx.cs
protected string GetValueTwoLink(object name, object value2)
{
ViewState["ListOfValues"] = datalayer.GetListOfValues(name, value2);
return string.Format(
"{1}",
name,
Convert.ToInt32(value2).ToString("d"));
}
Any help would be appreciated!
Not exactly sure what you are trying to do with that onclick... but you can do it on the code-front:
<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
<td><%# Eval("ID") %></td>
<td><%# Eval("Value1") %></td>
<td>
<a href="#"
onclick="<%# String.Format("window.opener.ViewValues('{0}')", DataBinder.Eval(Container.DataItem,"Name")) %>">
<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Value2")).ToString("d")%>
</a>
</td>
</tr>
</ItemTemplate>
Also, as you see, you can just use
<%# Eval("Name") %>
instead of
<%# DataBinder.Eval(Container.DataItem,"Name") %>
to just show data. You use the DataBinder.Eval when you need to do some manipulation, like in the 4th <td>.