asp Repeater ItemTemplate - eval whole object - asp.net

Currently I am using asp:Repeater like this:
<asp:Repeater ID="itemsRepeater" runat="server">
<ItemTemplate>
<my:Button runat="server" Title='<%# DataBinder.Eval(Container.DataItem, "Title") %>' />
</ItemTemplate>
</asp:Repeater>
But now, I would like to send whole model to my:Button control like:
<asp:Repeater ID="itemsRepeater" runat="server">
<ItemTemplate>
<my:TabListButton runat="server" Model='<%# this %>' />
</ItemTemplate>
</asp:Repeater>
Could you tell me how to handle that?

I have solved this problem... somehow.
Maybe it's the only way to handle that. I know it's not beautiful, but it works:
public ModelType Model
{
get
{
return this;
}
}
and then:
<asp:Repeater ID="itemsRepeater" runat="server">
<ItemTemplate>
<my:Item runat="server" Model='<%# DataBinder.Eval(Container.DataItem, "Model") %>' />
</ItemTemplate>
</asp:Repeater>

Related

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>

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>

conditional markup in control's template

I have a repeater control on my .aspx page. There are times where a product is unique, so you can't change it's quantity, but in other cases when there are lots of items of this product, you should be able to edit it's quantity using a textbox and a linkbutton. Both OnlyOne and Quantity are present in the binded collection classes. I need to check the OnlyOne condition, something like that:
<% if (OnlyOne) { %>
<%# Eval("Quantity") %>
<%} else { %>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server">OK</asp:LinkButton>
<% }%>
The problem is that ASP.NET doesn't find the OnlyOne field. I've tried also (bool)Eval("OnlyOne"), but that didn't work also. So how should I write the condition?
Done it like that:
<asp:Label Text='<%# Eval("Quantity") %>' runat="server" ID="QuantityLabel" Visible='<%# Eval("OnlyOne") %>' />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Quantity") %>' Visible='<%# !(bool)Eval("OnlyOne") %>' />
<asp:LinkButton ID="LinkButton1" runat="server" Visible='<%# !(bool)Eval("OnlyOne") %>' Text="OK" />
But I am still interested to hear the answer :) .

ASP.NET ItemTemplate Container.DataItem

I have a Repeater on one of my pages like so:
<asp:Repeater ID="rptrHalls" runat="server" OnItemCommand="Choose_Hall">
<ItemTemplate>
<asp:Button ID="btn<% Container.DataItem %>" runat="server"
CommandName="<% Container.DataItem %>" Text="<% Container.DataItem %>"
/>
</ItemTemplate>
</asp:Repeater>
But, when I run it it errors out with the message:
'btn<% Container.DataItem %>' is not a valid identifier.
I want to append btn to the Container.DataItem value so that I have dynamically assigned control names that are associated with the underlying data item. Any ideas?
It should be something like
<asp:Button ID='<%# "btn" + Container.DataItem %>' runat="server"
and depends on the type of Container.DataItem
but is there a reason why you want to set the ID and not use something like this ?
<asp:Button ID="btnSubmit" runat="server"

Serving an image with a generic handler within a list view. Is it possible?

Currently in my webpage i load images to the ListView object as follows...
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server">
<layouttemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" />
</layouttemplate>
<ItemTemplate>
<td>
<asp:Image ID="Image1" runat="server"
ImageUrl = '<%# DataBinder.Eval(Container.DataItem, "Image") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
Now, i would like to use a combination of a Generic Handler and the ListView object to serve the images into the ListView
...the generic handler call is like
~/Handlers/Image.ashx?img=
How could i combine both above to serve images?
I tried something like the following but it is not correct
<asp:Image ID="Image1" runat="server"
ImageUrl = ~/Handlers/Image.ashx?img= & '<%# DataBinder.Eval(Container.DataItem, "Image") %>' />
So what is the correct way?
Yes that is the correct way. Your syntax for binding the ImageUrl not correct though.
Try this one:
<asp:Image ID="Image1" runat="server" ImageUrl ='<%# "~/Handlers/Image.ashx?img=" + Eval("Image")%>' />
You might also use the ItemDataBound event to use code like this:
Image image1 = e.FindControl("Image1") as Image;
YourClass item = e.DataItem as YourClass;
image1.ImageUrl = String.Format("~/Handlers/Image.ashx?img={0}", item.Image")
Try this
<ItemTemplate>
<asp:Hyperlink runat= "server" Text='<%#DataBinder.Eval(Container.DataItem,"ProductName").ToString()%>' NavigateUrl='<%# "page.aspx?Name=" & DataBinder.Eval (Container.DataItem,"ProductName").tostring %>' ID="ProductName"/>
</ItemTemplate>
Hope it helps
Source : http://www.extremeexperts.com/Net/FAQ/PassingMulitpleParameterinURLLink.aspx

Resources