Can I nest Templates in a Web User Control? - asp.net

I want to do something like this:
<MyTemplate>
<span><%# Container.Title %></span>
<MySubTemplate>
<span><%# Container.Username %></span>
</MySubTemplate>
</MyTemplate>
Assuming I have a list of Titles that each have a list of Usernames.. If this is a correct approach how can I do this or what is a better way?

If you have a list of titles, that each have their own list of UserNames, it seems like you want to do something with nested repeaters (or other controls), not templates...
<asp:Repeater ID="rptTitle" runat="server" >
<ItemTemplate>
<%# Eval("Title") %>
<asp:Repeater ID="rptUsers" runat="server" >
<ItemTemplate>
<%# Eval("UserName") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
And then bind the rptUsers during the ItemDataBound event of rptTitle...

You can do it this way. You can also use:
Labels
Span runat="server" and add them programmatically
(ghetto) string.replace

Related

Validating a control inside a nested repeater

I think my subject line pretty much asks the question. I have a control inside a nested repeater that I need to validate.
Here's a quick 'n dirty as to how it looks (note: attributes/lines left out for brevity):
<asp:Repeater ID="outsideRepeater">
<ItemTemplate>
<asp:Repeater ID="middleRepeater">
<ItemTemplate>
<asp:Repeater ID="insideRepeater">
<ItemTemplate>
<asp:TextBox ID="someDate" CausesValidation="true" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
<asp:RegularExpressionValidator ID="valSomeDate" ControlToValidate="someDate">
</asp:RegularExpressionValidator>
The number of times that "someDate" can repeat is irrevelant; the point is that "someDate" can repeat.
When I run this, I get:
Unable to find control id 'someDate' referenced by the
'ControlToValidate' property of 'valSomeDate'.
How do I get around this?
Thanks!

How to set a download counter on Hyperlink Control inside Repeater? asp.net

I have a hyperlink inside a repeater control for the list of pdf. I want to set a download counter on each click on each hyperlink. The FileName must be a parameter. My code is basically like below. There is also code that calls stored procedure and bind it to the repeater on page_load.
<asp:Repeater ID="rptPDF" runat="server">
<ItemTemplate>
<div class="repeaterResources">
<b><%# Eval("Name") %></b><br />
<b>Description</b> <%# Eval("Description") %><br />
<asp:HyperLink ID="HyperLink2" runat="server" class="downloadLink" NavigateUrl='<%# "~/PDF/" & Eval("Filename") %>' Target="_blank">Download</asp:HyperLink><br /><br />
</div>
</ItemTemplate>
</asp:Repeater>
The mystery bit is how to get a button click event from here. Thanks.
You can use the OnCommand event and set the CommandArgument attribute with a value using
<%# Eval('myvalue') %>.
MSDN has an example minus the repeater: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx
Sample:
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="button1" OnCommand="button1_command" CommandArgument='<%# Eval("myvalue") %>' />
</ItemTemplate>
</asp:Repeater>

Use SPListeItem properties inside an asp.net repeater

I am using <asp:repeater> to display the items from a sharepoint list.
foreach (SPListItem curItem in curItems)
{
_itemUrl = curItem.Web.Url;
_listUrl = curItem.ParentList.Form[PAGETYPE.PAGE_DISPLAYFORM].Url;
_itemId = curItem.ID;
}
ASP.net repeater:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# ((SPListItem)Container.DataItem)["Title"] %>
</ItemTemplate>
</asp:Repeater>
How can I display these properties:curItem.Web.Url, curItem.ParentList.Form[PAGETYPE.PAGE_DISPLAYFORM].Url in the repeater like I did for "Title" field?
Like this:
<%# ((SPListItem)Container.DataItem)["Title"] %>
Any idea how it can be done and what is the best way of doing it?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# ((SPListItem)Container.DataItem).Web.Url %>
</ItemTemplate>
</asp:Repeater>
OR
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# ((SPListItem)Container.DataItem).ParentList.Form[PAGETYPE.PAGE_DISPLAYFORM].Url %>
</ItemTemplate>
</asp:Repeater>
If you cast the data container, you can directly use the object as such.

ASP.NET repeater over list of hyperlinks

I am making a pagination feature, because the default one in webforms uses postbacks, which is pathetic really.
Just wondering if there is a better way to output this List of links.
paginator = new Paginator(10,35);
// List<HyperLink>
rptPagination.DataSource = paginator.getPageLinks();
<asp:Repeater ID="rptPagination" runat="server">
<ItemTemplate>
<%# Eval("Text") %>
</ItemTemplate>
</asp:Repeater>
Obviously if I try to change other properties of the HyperLinks, like target, visible etc this will not be rendered into the page.
You can keep using that syntax and continue with pure HTML:
<ItemTemplate>
<%# Eval("Text") %>
</ItemTemplate>
Or, a server side version:
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%# Eval("NavigateUrl") %>'
Text='<%# Eval("Text") %>' />
</ItemTemplate>

Can I define Default Sort order in LinQ

If I have a nested ListView, and I'm calling a related table in LinQ, how do I sort it, without resorting to the ItemDataBound event of the parent?
Pseudo Code (UPDATED WITH SOLUTION):
<asp:ListView ID="lv" runat="server" OnItemDataBound="lv_ItemDataBound" >
<LayoutTemplate>
<!-- Product Category Stuff -->
<asp:PlaceHolder Id="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:ListView ID="lvInner" runat="server" DataSource='<%# <%# ((Category)Container.DataItem).Products.OrderBy(p => p.Description) %> %>'>
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>Item Stuff</li>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
Perhaps the method is deceptively simple, but I want the inner Products to be sorted by a field. I can't see a way to do it declaratively as LinQ creates this Query on the fly, if I'm not mistaken, and doesn't do sorting.
Any thoughts?
UPDATE
Updated the Example to the following:
<%# ((Category)Container.DataItem).Products.OrderBy(p => p.Description) %>
Hope it helps someone else!
My assumption is that Products is an IEnumerable<Product> (or IQueryable). If that is the case, why not just add the OrderBy method to the evaluation, like so:
<%# Eval("Products.OrderBy(p => p.FieldToSortOn)") %>

Resources