I'm using an ASP.NET Repeater to display the contents of a <table>. It looks something like this:
<table cellpadding="0" cellspacing="0">
<asp:Repeater ID="checkboxList" runat="server" OnItemDataBound="OnCheckboxListItemBound">
<ItemTemplate>
<tr id="itemRow" runat="server">
<td>
Some data
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
It works fine, but i'd like to have an if() statement inside the ItemTemplate so i can conditionally determine if i want to print out a <tr> tag.
So i'd like to have something like this:
<table cellpadding="0" cellspacing="0">
<asp:Repeater ID="checkboxList" runat="server" OnItemDataBound="OnCheckboxListItemBound">
<ItemTemplate>
<% if ( (CurrentItemCount % 2) == 0 ) { %?>
<tr id="itemRow" runat="server">
<% } %>
<td>
Some data
</td>
<% if ( (CurrentItemCount % 2) == 0 ) { %?>
</tr>
<% } %>
</ItemTemplate>
</asp:Repeater>
</table>
Is there some way i can achieve this?
PS. The CurrentItemCount is just made up. I also need a way to get the current item count inside that if() statement. But i only seem to be able to get it from <%# Container.ItemIndex; %>, which can't be used with an if() statement?
Another way of doing this (if performance is not a problem):
<ItemTemplate>
<!-- "If" -->
<asp:PlaceHolder runat="server" Visible="<%# MyCondition %>">
<tr><td></td></tr>
</asp:PlaceHolder>
<!-- "Else" -->
<asp:PlaceHolder runat="server" Visible="<%# !MyCondition %>">
<tr><td></td></tr>
</asp:PlaceHolder>
</ItemTemplate>
If you're trying yo make a 2 columns table this could do the trick
<%# Container.ItemIndex % 2 == 0 ? "<tr class='itemRow'>" : "" %>
<td>
Some data
</td>
<%# Container.ItemIndex % 2 != 0 ? "</tr> : "" %>
Changed a couple of things: id="itemRow" for all rows would cause repeated ids what is not allowed.
Removed runat="server" since doesn't make sense on this context.
I have 2 examples, for the examples i will bind the repeater to a array of strings (demonstration purposes only)
void BindCheckboxList()
{
checkboxList.DataSource = new string[] { "RowA", "RowB", "RowC", "RowD", "RowE", "RowF", "RowG" };
checkboxList.DataBind();
}
Example 1: Create a methode in de codebehind casting the bound elements back en evaluate what ever value you'd like.
Create Methode in CodeBehind (example 1):
protected string StringDataEndsWith(object dataElement, string endsWith, string returnValue)
{
// for now an object of the type string, can be anything.
string elem = dataElement as string;
if (elem.EndsWith(endsWith))
{
return returnValue;
}
else
{
return "";
}
}
In the .aspx file (example 1):
<asp:Repeater ID="checkboxList" runat="server">
<HeaderTemplate>
<table style="padding:0px;margin:0px;">
</HeaderTemplate>
<ItemTemplate>
<%# StringDataEndsWith(Container.DataItem,"A","<tr id=\"itemRow\" runat=\"server\">") %>
<td>
<%# Container.DataItem %>
</td>
<%# StringDataEndsWith(Container.DataItem,"G","</tr>") %>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Example 2: You could use a direct cast in the .aspx file
DirectCast example (no code behind):
<asp:Repeater ID="checkboxList" runat="server">
<HeaderTemplate>
<table style="padding:0px;margin:0px;">
</HeaderTemplate>
<ItemTemplate>
<%# Convert.ToString(Container.DataItem).EndsWith("A") ? "<tr id=\"itemRow\" runat=\"server\">" : "" %>
<td>
<%# Container.DataItem %>
</td>
<%# Convert.ToString(Container.DataItem).EndsWith("G") ? "</tr>" : "" %>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I hope this is what you're looking for. Regards.
If you're wanting to do something on every other item, use the alternating item template.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate.aspx
I would use codebehind:
protected void OnCheckboxListItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlTableRow itemRow = (HtmlTableRow) e.Item.FindControl("itemRow");
itemRow.Visible = e.Item.ItemIndex % 2 == 0;
}
}
Related
I am having problem with the LinkButton onclick event not firing.
I have checked the following posts and taken the precaution of Postback but still joy
repeater linkbutton not firing
Repeater's Item command event is not firing on linkbutton click
Here is my Code so far
<asp:PlaceHolder runat="server" ID="phOrders">
<asp:Repeater ID="rprOrders" runat="server" OnItemCommand="rprOrders_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
<asp:LinkButton ID="lnkOrderByDate" runat="server" Text="Date" CommandName="OrderDate" OnClick="lnkOrderByDate_Click"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByOrderNumber" runat="server" Text="Order Number"></asp:LinkButton></th>
<th>
<asp:LinkButton ID="lnkOrderByProductNumber" runat="server" Text="Product Number"></asp:LinkButton></th>
<th>Product Description</th>
<th>Size</th>
<th>QTY</th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong><%# Eval("OrderDate") %></strong></td>
<td><%# Eval("OrderNumber") %></td>
<td><%# Eval("SKUNumber") %></td>
<td><%# Eval("OrderItemSKUName") %></td>
<td><%# Eval("mtrx_Code2") %></td>
<td><%# Eval("OrderItemUnitCount") %></td>
<td><strong><%# Eval("OrderItemStatus") %></strong></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div class="track-footer"></div>
</asp:PlaceHolder>
Code Behind
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
if (CMSContext.ViewMode == ViewModeEnum.LiveSite)
{
if(!Page.IsPostBack)
{
PopulateProductClass();
PopulateProduct();
PopulateDefaultViewOrders();
}
}
}
}
protected void lnkOrderByDate_Click(object sender, EventArgs e)
{
//Do Something
}
Any suggestions? I can't seem to figure it out?
Even the OnItemCommand="rprOrders_ItemCommand" wont fire either?
The LinkButton within your DataControl triggers the Method rprOrders_ItemCommand
Set a breakpoint there. If you have multiple LinkButton then you can read CommandName="OrderDate" Codebehind: (e.CommandName)
For passing values CommandArgument should be used.
use some thing like this
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
.cs
protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("MyUpdate"))
{
// some code
}
if (e.CommandName.Equals("EditCategory"))
{
// some code
}
}
For me the fix was to set EnableViewState="true" the the control tag of my ascx file.
E.G.
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="Settings.ascx.cs" Inherits="Foo.Bar.Settings" EnableViewState="true" %>
And my LinkButton looks like this
<asp:LinkButton ID="btnRemoveMedia"
runat="server"
class="icon mdi-delete"
OnCommand="btnRemoveMedia_OnCommand"
CommandArgument="<%# ((Tuple<int, string>) Container.DataItem).Item1 %>"
CommandName="Delete"
UseSubmitBehavior="false" />
I'm needing a solution like this one https://stackoverflow.com/a/7368719/1203098 where my link that I need to select is in a ListView that is group by inside another ListView.
The error I keep getting is an "Object reference not set to an instance of an object"
<asp:ListView ID="lstViewSharedFolder" runat="server" OnItemDataBound="lstViewSharedFolder_ItemDataBound">
<ItemTemplate>
<tr class="alert-info">
<td><strong>Folder ID</strong>:
<%# Eval( "FolderID") %>
</td>
<td><strong>Folder Name</strong>:
<%# Eval( "SharedFolderName") %>
</td>
<td><strong>Security Score</strong>:
<%# Eval( "Score") %>
</td>
</tr>
<tr>
<td colspan="3">
<asp:ListView ID="lstViewUsers" runat="server" OnItemDataBound="lstViewUsers_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:HyperLink ID="lnkUsername" runat="server">
<%# Eval( "UserName") %>
</asp:HyperLink>
</td>
<td>
<%# Eval( "ReadOnly") %>
</td>
<td>
<%# Eval( "Give") %>
</td>
<td>
<%# Eval( "CanAdminister") %>
</td>
<td>
<%# Eval( "GroupName") ?? "n/a" %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#<%= ((HyperLink)((ListView)lstViewSharedFolder.FindControl("lstViewUsers")).FindControl("lnkUsername")).ClientID %>').click(function() {
var id = $(this).attr("id"); // Get the ID
alert(id);
console.log(id);
});
});
</script>
EDIT
I think I know why. I'm binding the Users ListView control when the SharedFolder Item is being bound so maybe the control doesn't exist yet. Is there a better more efficient way to do this? My goal was to display a popup with the user details when the link was clicked.
var folderList = $('#<%= lstViewSharedFolder.ClientID %>');
<!-- This causes the error -->
var userList = $('#<%= ((ListView)lstViewSharedFolder.FindControl("lstViewUsers")).ClientID %>');
Here's my lstViewSharedFolder_ItemDataBound().
protected void lstViewSharedFolder_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var folder = (SharedFolder)dataItem.DataItem;
var users = folder.Users;
var lstView = (ListView)e.Item.FindControl("lstViewUsers");
lstView.DataSource = users;
lstView.DataBind();
}
}
No need to complicate things here. Just give the LinkButton a CssClass.
<asp:HyperLink ID="lnkUsername" runat="server" CssClass="my_link">
<%# Eval( "UserName") %>
</asp:HyperLink>
and call it like
var userList = $('.my_link');
I have a repeater to show an order confirmation. It is bound to a table generated with Entity Framework code first. The table contains the right information and the repeater shows all the information right except for the part where I show the totals which I paste below.
I believe that this part also works okay but quantity does not. For example if I add 5 products it will only show one, but if I put a break point and I run the code in debug mode I see that the value inserted in the Quantity table is 5 not 1, so the Quantity value on the table is inserted correctly, but the repeater reports 1.
Here is the code:
<asp:Repeater ID="rptConfirmOrder" runat="server">
<ItemTemplate>
<fieldset class = "OrderConfirmationFieldset"><legend class ="OrderDataLegend">Order Summary</legend>
<td align="left" width="60%" runat="server" id="Td25">
<asp:Label ID="lblQuantity" runat="server" Text="Quantity: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Quantity") %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td26">
<asp:Label ID="lblProductName" runat="server" Text="Product Name: " CssClass = "lblOrderConfirmation">
</asp:Label><%# Eval("ProductName" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td27">
<asp:Label ID="lblProductPrice" runat="server" Text="Product price: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("ProductPrice" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td28">
<asp:Label ID="lblSubtotal" runat="server" Text="Subtotal: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Subtotal" ,"{0:c}" ) %>
<br />
</td>
<td align="left" width="60%" runat="server" id="Td29">
<asp:Label ID="lblTotal" runat="server" Text="Total: " CssClass = "lblOrderConfirmation">
</asp:Label> <%# Eval("Total" ,"{0:c}" ) %>
<br />
</td>
</ItemTemplate>
</asp:Repeater>
Can Anyone help?
Thank you in advance!
Your repeater markup doesn't look right.
have it as follows:
<asp:Repeater ID="rptConfirmOrder" runat="server" OnItemDataBound="rptConfirmOrder_ItemDataBound">
<ItemTemplate>
// stuff
</ItemTemplate>
</asp:Repeater>
And if you have it that way, and this was a paste error, then forget the value in the database/entity etc.
You can verify accurately, what value is being bound by tapping into the row data bound event as follows..
protected void rptConfirmOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var dataItem = e.Item.DataItem as YOUR_ENTITY_TYPE;
Debug.Assert(5 == dataItem.Quantity);
}
}
I think you may need to add the < itemtemplate > tags in after the opening repeater and close it off before the closing repeater tag.
I am trying to input logic in the source view in Asp.Net ListView. The problem is that the program is writing on the screen false or true when executing "If (isItTrue(test))". Does anyone know how to solve this problem?
<%# test= Eval("testId")%>
<%
If (isItTrue(test)) Then
%>
<asp:Button ID="btnTest" runat="server" Text="Like" />
<%
Else
%>
<asp:Label runat="server" Text="hello" </asp:Label>
<%
End If
%>
You could use ItemDataBound to check informations like this and show or hide the controls using your condition. try something like this in your code behine:
protected void ListViewTest_ItemDataBound(object sender, ListViewItemEventArgs e)
{
// if it is data item
if (e.Item.ItemType == ListViewItemType.DataItem)
{
// call your function
if (isItTrue("test"))
{
// show the button
e.Item.FindControl("btnTest").Visible = true;
}
else
{
// show the label
e.Item.FindControl("lblTest").Visible = true;
}
}
}
And in your Listview, you could do something like this, setting the event and adding the controls on the place holder
<asp:ListView ID="ListViewTest" DataSourceID="..." OnItemDataBound="ListViewTest_ItemDataBound" runat="server">
<LayoutTemplate>
<table>
<tr>
<th>Column Name</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #CAEEFF" runat="server">
<td>
<%-- both controls are here --%>
<asp:Button ID="btnTest" runat="server" Visible="false" Text="Like"></asp:Button>
<asp:Label ID="lblTest" runat="server" Visible="false" Text="hello"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Are you sure it's not this line: <%# test= Eval("testId")%> that is writing true or false to the output?
I want to show certain parts of an ItemTemplate based according to whether a bound field is null. Take for example the following code:
(Code such as LayoutTemplate have been removed for brevity)
<asp:ListView ID="MusicList" runat="server">
<ItemTemplate>
<tr>
<%
if (Eval("DownloadLink") != null)
{
%>
<td>
Link
</td>
<%
} %>
</tr>
</ItemTemplate>
</asp:ListView>
The above gives the following run-time error:
Databinding methods such as Eval(),
XPath(), and Bind() can only be used
in the context of a databound control.
So how can put some conditional logic (like the above) in an ItemTemplate ?
What about binding the "Visible" property of a control to your condition? Something like:
<asp:ListView ID="MusicList" runat="server">
<ItemTemplate>
<tr runat="server" Visible='<%# Eval("DownloadLink") != null %>'>
<td>
<a href='<%#Eval("DownloadLink") %>'>Link</a>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
To resolve "The server tag is not well formed." for the answers involving visibility, remove quotes from the Visible= parameter.
So it will become:
<tr runat="server" Visible=<%# Eval("DownloadLink") != null ? true : false %>>
I'm not recommending this as a good approach but you can work around this issue by capturing the current item in the OnItemDataBound event, storing it in a public property or field and then using that in your conditional logic.
For example:
<asp:ListView ID="MusicList" OnItemDataBound="Item_DataBound" runat="server">
<ItemTemplate>
<tr>
<%
if (CurrentItem.DownloadLink != null)
{
%>
<td>
Link
</td>
<%
} %>
</tr>
</ItemTemplate>
</asp:ListView>
And on the server side add the following code to your code behind file:
public MusicItem CurrentItem { get; private set;}
protected void Item_DataBound(object sender, RepeaterItemEventArgs e)
{
CurrentItem = (MusicItem) e.Item.DataItem;
}
Note that this trick will not work in an UpdatePanel control.
If you have 2 different structure that are to be rendered according to a condition then use panels
<asp:ListView ID="MusicList" runat="server">
<ItemTemplate>
<tr>
<asp:Panel ID="DownloadNull" runat="server" Visible="<%# Eval("DownloadLink") == null %>" >
<td> Album Description BlaBlaBla <img src="../images/test.gif"> </td>
</asp:Panel>
<asp:Panel ID="DownloadNotNull" runat="server" Visible="<%# Eval("DownloadLink") != null %>" >
<td> Album Description BlaBlaBla <img src="../images/test.gif">
<a href='<%# Eval("DownloadLink")' >Download</a>
.....
</td>
</asp:Panel>
</tr>
</ItemTemplate>
</asp:ListView>