ASP.NET ItemTemplate Container.DataItem - asp.net

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"

Related

asp Repeater ItemTemplate - eval whole object

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>

Asp.net web form model binding "Item" and "BindItem" same time to 1 control's multiple attributes/properties

I am using strongly typed with a Gridview,
This will work (1 BindItem)
<ItemTemplate>
<asp:TextBox runat="server" Text="<%# BindItem.Price %>" />
</ItemTemplate>
This will work (1 Item)
<ItemTemplate>
<asp:TextBox runat="server" Text="" Enabled="<%# Item.Price==5 %>" />
</ItemTemplate>
This will not work (1 BindItem and 1 Item)
<ItemTemplate>
<asp:TextBox runat="server" Text="<%# BindItem.Price %>" Enabled="<%# Item.Price==5 %>" />
</ItemTemplate>
CS0103: The name 'Item' does not exist in the current context
It seems once we use BindItem in 1 place, we can't use binding in with this control again.
I know I can wrap the textbox with a panel to control the "Enabled" property as a fix
Replace Item.Price==5 with BindItem.Price==5.

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>

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 :) .

Selecting items of a Listview using checkboxes in ASP .NET

I am trying to use checkboxes to select the items in a Listview. I have added a checkbox control in the , and they are displayed properly.
The problem is that Checked property never changes when I click on them. Why does this happen? And is there a workaround?
Here is the code:
<asp:ListView ID="ListView1" runat="server"
onitemcommand="ListView1_ItemCommand"
onitemdatabound="ListView1_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" OnCheckedChanged="CheckBoxSelect_checkchanged"/>
<asp:LinkButton ID="LinkButtonOpen" CommandArgument='<%#Eval("MessageID") %>' runat="server">
<asp:Label ID="Label1" Text="[]" runat="server"/>
<%#Eval("FirstName" )%> <%#Eval("LastName")%>
<%#Eval("Subject") %>
<%#Eval("Timestamp") %>
<asp:HiddenField runat="server" ID="ReadStatus" Value='<%#Eval("IsRead") %>' />
</asp:LinkButton>
</ItemTemplate>
</asp:ListView>
I think you are missing the AutoPostBack="true"
<asp:CheckBox ID="CheckBoxSelect" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxSelect_checkchanged"/>
The problem was that I was binding the ListView during page load. When that happened, the checkboxes would get cleared, and I got the Checked property as False in all subsequent functions.

Resources