Is there any way to display listview as another listview's empty data template. I just put it like following. But it wont display the listview or it's emptydata text. only displaying 'empty data first listview' text
<asp:ListView ID="searchResults" runat="server" ItemPlaceholderID="placeholder">
<EmptyDataTemplate>
empty data first listview
<asp:ListView ID="suggestions" runat="server" ItemPlaceholderID="placeholder" DataSource="<%#Model.SearchSuggestions %>">
<EmptyDataTemplate>
empty data second listview</EmptyDataTemplate>
<LayoutTemplate>
<span class="suggestionList">
<asp:PlaceHolder ID="placeholder" runat="server"></asp:PlaceHolder>
</span>
</LayoutTemplate>
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
<ItemSeparatorTemplate>
,</ItemSeparatorTemplate>
</asp:ListView>
</EmptyDataTemplate>
<LayoutTemplate>
*************
</LayoutTemplate>
</asp:ListView>
I copy and pasted your code and was able to get it to work by manually databinding it in the codebehind. The only modification I made to your aspx code was to remove the reference to the model, to simplify setting it up on my end. I have included my ugly/overly simple test code below.
protected void Page_Load(object sender, EventArgs e)
{
searchResults.DataBind();
searchResults.Controls[0].DataBind();
}
Related
I have a ListView. At the moment I have to click in a Button to show the InsertItemTemplate, as shown below.
Default.aspx:
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="ItemPlaceHolder" runat="server" />
</ul>
<asp:Button ID="New" Text='New' CommandName="Create" OnClick="Novo_Click" runat="server" />
</LayoutTemplate>
Default.aspx.cs:
protected void Novo_Click(object sender, EventArgs e)
{
ListViewID.InsertItemPosition = InsertItemPosition.LastItem;
}
How can I have the InsertItemTemplace visible without needing to click a button?
Just add that line in the ListView declaration: InsertItemPosition="LastItem" to show, the template, at the end or
InsertItemPosition="FirstItem" to show, the template, in the top
The default is InsertItemPosition.None, which indicates that the InsertItemTemplate content will not be rendered by the ListView control.
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemInserted="ContactsListView_ItemInserted"
InsertItemPosition="LastItem"
runat="server">
...
</asp:ListView>
I want to sort the list view by a certain XML Element.
Here is my aspx code which shows how the listview is set up:
<asp:ListView ID="ListView1" runat="server" DataSourceID="newsDataSource">
<LayoutTemplate>
<div id="ItemPlaceHolderContainer" runat="server">
<span id="ItemPlaceHolder" runat="server" />
</div>
</LayoutTemplate>
<ItemTemplate>
<h2><%# XPath("title") %></h2>
<p><%# XPath("contents") %></p>
<p>Author: <%# XPath("author") %></p>
<p>Date Time: <%# XPath("datetime") %></p>
</ItemTemplate>
</asp:ListView>
<asp:XmlDataSource ID="myDataSource" runat="server" DataFile="~/Data.xml">
</asp:XmlDataSource>
Here is an example of the XML:
<?xml version="1.0" encoding="utf-8"?>
<theNews>
<news>
<title>Welcome!</title>
<contents>Hello, and welcome!</contents>
<author>Me</author>
<datetime>6/11/2014 4:40:35 PM</datetime>
</news>
</theNews>
What I want to do is sort the content in the ListView according to the 'datetime' XML element. Anyone know how I can do this?
protected void Page_Load(object sender, EventArgs e)
{
// TODO: Sort me here!!
}
You need to add this line into your Layout Template, either manually or add it to your code to populate it within the ItemPlaceHolder.
<asp:LinkButton runat="server" ID="SortByDateTime"
CommandName="Sort" Text="Date Time" CommandArgument="datetime"/>
My goal is to nest a Repeater inside an ASP.Net AJAX Accordion's AccordionPane.
So there is one Accordion which I am programmatically adding AccordionPanes to. The amount of panes I add depends on my particular dataset's count value, usually no more than 5. I've managed to do this successfully.
The thing I am having difficulty with is creating and adding a Repeater per each AccordionPane.
I've glanced over http://iridescence.no/post/Using-Templated-Controls-Programmatically.aspx but this is not exactly what I had in mind. Instead, I would rather declare a single Repeater as static HTML that I could then "clone" when I need. How can I achieve this? Obviously I would want each control's ID (within this declared Repeater) to be generated automatically each time i "clone" it.
The repeater looks like this:
<asp:Repeater ID="rptForum" runat="server">
<ItemTemplate>
<div runat="server" style="border:solid #d3d3d3 1px; border-bottom-width:0px;">
<table width="100%">
<tr><td align="left">
<asp:Label runat="server" Font-Size="12px" />
</td></tr>
<tr><td align="left">
>> <asp:Label runat="server" Font-Size="12px" Text='<%# Eval("query") %>' />
</td></tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
I might add more fields to be databound as I progress.
Any ideas appreciated..
You should be able to do that by adding the repeater to the Content template:
<cc1:AccordionPane ID="AccordionPane1" runat="server">
<Header>
Foo
</Header>
<Content>
<asp:Repeater ID="Repeater1" runat="server" ...>
...
</asp:Repeater>
</Content>
</cc1:AccordionPane>
You can do a hierarchical data bind with the accordion, as illustrated here: http://aspalliance.com/1674_complex_data_binding_with_the_accordion_control
In my project the master page contains a repeater that's used as a menu with an Xml file as the data source for the repeater.
<asp:Repeater ID="Admin_menus" runat="server">
<HeaderTemplate><div id="navmenu"><ul></HeaderTemplate>
<FooterTemplate>|</ul></div></FooterTemplate>
<ItemTemplate>
|<li>
<a href="<%# DataBinder.Eval(Container.DataItem, "url")%>"
class="link6" id="<%# DataBinder.Eval(Container.DataItem, "id")%>">
<strong>
<%# DataBinder.Eval(Container.DataItem, "title")%>
</strong>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
urls in the xml file is as
<menuitems>
<item id="1" url="Employee.aspx" title="Employee" description="Employee" />
<item id="2" url="Location.aspx" title="Location" description="Location" />
</menuitems>
Here I want to change the CSS style of the current page in the menu.
One solution you can opt for is to handle the ItemCreated event of the <asp:Repeater> control. To do this you need to add an event handler:
In the .master markup:
<asp:Repeater ID="Admin_menus" runat="server" OnItemCreated="Admin_menus_ItemCreated">
<HeaderTemplate>
<div id="navmenu">
<ul>
</HeaderTemplate>
<FooterTemplate>
|</ul></div></FooterTemplate>
<ItemTemplate>
|<li runat="server" id="hyperlink"><a href="<%# DataBinder.Eval(Container.DataItem, "url")%>" class="link6" id="<%# DataBinder.Eval(Container.DataItem, "id")%>">
<strong>
<%# DataBinder.Eval(Container.DataItem, "title")%></strong> </a></li>
</ItemTemplate>
</asp:Repeater>
In the .master.cs codebehind:
protected void Admin_menus_ItemCreated(object sender, RepeaterItemEventArgs e)
{
// Ensure that the ItemCreated is not null, the first one (header?) gets
// returned null
if (e.Item.DataItem != null)
{
// Extract the "url" attribute from the Xml that's being used for
// databinding for this particular row, via casting it down to
// IXPathNavigable as the concrete type of e.Item.DataItem isn't available
// to us.
var currentUrl = ((IXPathNavigable)e.Item.DataItem).CreateNavigator().GetAttribute("url", "");
if (Request.Url.PathAndQuery.Contains(currentUrl))
{
// This just adds a background color of "red" to the selected
// menu item. What you actually do is entirely up to you
var hyperlink = (HtmlGenericControl) e.Item.FindControl("hyperlink");
hyperlink.Style.Add(HtmlTextWriterStyle.BackgroundColor, "red");
}
}
}
Note that I've added a runat="server" as well as an id="hyperlink" to the <li> tag in your ItemTemplate so that the code in the ItemCreated handler can find it easily to style it.
Perhaps one solution is to check the current page in your inline Eval code and add the "currentpage" class to the anchor
For simplicity I'm using Eval() instead of DataBinder.Eval()
<asp:Repeater ID="Admin_menus" runat="server">
<HeaderTemplate>
<div id="navmenu"><ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<%# Eval("url") %>' class='link6<%# Request.RawUrl.EndsWith(Eval("url").ToString()) ? " currentpage" : "" %>' id='<%# Eval("id")%>'><strong><%# Eval("title")%></strong></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></div>
</FooterTemplate>
</asp:Repeater>
Instead of binding directly from your xml data to repeater, try to get the repeater databing event, and compare with the current page url and the each binding item to determine if the current item should be hi-lighted
I want to be able to get the current bound object in the ItemTemplate of a ListView control.
Here's an example of what I want to do:
<asp:ListView ID="UserList" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
//How can I get the current bound object in here?
</ItemTemplate>
</asp:ListView>
You can access it via the DataItem:
<%# DataBinder.Eval(Container.DataItem, "myPropertyName")%>'
If you wanted a textbox for example:
<asp:Label ID="MyProp" runat="server" Text='<%#Eval("myPropertyName") %>' />
If you just want the full object:
<%# (MyType)Container.DataItem %>