I have a <asp:repeater> inside which i'm rendering a table with a few rows.
Each row - corresponding to a different value.
Question: how to "skip" the row, in case this value is empty?
here is evaluation statement:
<%# DataBinder.Eval(Container.DataItem, "Website") == ""? "" : /*render element*/ %>
and here is element i want to render in case statement if false:
<tr>
<td><span>Website address:</span></td>
<td>
<p><%#DataBinder.Eval(Container.DataItem, "Website") %></p>
</td>
</tr>
Try this:
<asp:Repeater runat="server" id="myRepeater">
<ItemTemplate>
<tr runat="server"
visible='<%#String.Format("{0}",DataBinder.Eval(Container.DataItem, "Website"))!="" %>'>
<td><span>Website address:</span></td>
<td>
<p><a href='<%#DataBinder.Eval(Container.DataItem, "Website") %>"
class="red-link'><%#DataBinder.Eval(Container.DataItem, "Website") %></a></p>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
It will set the row's visible attribute to false when the Website is empty or null.
Related
I'm using bootstrap to collapse and expand a table, which is working fine but I'm using classes instead of IDs. With this, expanding one row expands all the rows rather than just that one. My question is how does my data-target point at a nested repeater id? The transactionCollapse ID is unable to be targeted directly and I've tried doing <%=transactionGroupedList.FindControl("transactionCollapse")%> but it threw an error.
<tbody>
<asp:Repeater runat="server" ID="transactionGroupedList" OnItemDataBound="TransactionGroupedDataList_ItemDataBound">
<ItemTemplate>
<tr>
<!-- This line should target the transactionCollapse ID below instead of the class -->
<td data-toggle="collapse" data-target=".transactionCollapse">
<span id="transactionGroupCollapseIcon" runat="server" class="fonticon-down-arrow"></span>
<custom:Label runat="server" ID="transactionActivityDataColumnLabel"></custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionDateDataColumnLabel">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionNumberDataColumnLabel">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionAmountDataColumnLabel">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionStatusDataColumnLabel">
</custom:Label>
</td>
</tr>
<asp:Repeater runat="server" ID="transactionDetailList" OnItemDataBound="TransactionDetailsDataList_ItemDataBound">
<ItemTemplate>
<tr id="transactionCollapse" runat="server" class="collapse transactionCollapse">
<td colspan="2">
<custom:Label runat="server" ID="transactionDetail">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionDetailTransactionNumber">
</custom:Label>
</td>
<td>
<custom:Label runat="server" ID="transactionDetailAmount">
</custom:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</tbody>
The Online Payment row is what collapses/expands the Posting - and MP Payment rows below. This user only has one Online Payment, but many users will have multiple.
You have a couple of problems. First of all when using FindControl inside a Repeater/GridView etc is index based. So you need to use FindControl on the correct Item.
transactionGroupedList[i].FindControl("transactionCollapse")
However the above will still not work because transactionCollapse is in a nested Repeater that needs to be found first and then access the correct Item Index.
transactionGroupedList.Items[0].FindControl("transactionDetailList").Items[0]...
But this will also not work since FindControl does not know that transactionDetailList is a Repeater with index based Items. So you need to cast the nested Repeater first before you can access it's items. So it becomes this
<%= ((Repeater)transactionGroupedList.Items[i].FindControl("transactionDetailList")).Items[i].FindControl("transactionCollapse").ClientID %>
I have bound a DataSet to a ListView. In the ListView ItemTemplate, if a row value is empty, I do not want it, or the <td> element it is enclosed in to display.
In my code, the first row value will display. However, when I try to use the If statement on the second <td> element, that will not display.
<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"> </asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"> </asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("textItem1") %>
</td>
<% if (!String.IsNullOrEmpty(textItem2){ %>
<td>
<%# Eval("textItem2") %>
</td>
<%} %>
</ItemTemplate>
</asp:ListView>
That If statement works in an aspx page if its NOT being used in a ListView, Repeater, or GridView (or does it?). I need to be able to check if that string row value is empty and not display it or the <td> element it is enclosed in. I am not against a solution that uses code-behind. Is there another way to do this then my attempt?
I used a pretty simple method solution..
In the above code, I swapped out this...
<% if (!String.IsNullOrEmpty(textItem2){ %>
<td>
<%# Eval("textItem2") %>
</td>
<%} %>
For this...
<%# writeText(Eval("textItem2").ToString())%>
And placed this method in the code behind...
public string writeText(string kiss)
{
if (!String.IsNullOrEmpty(kiss))
return "<td> " + kiss + "</td>";
else
return null;
}
I have this and I want to hide rows dynamically using vb.net codebehind.
I am using VS2010.
This is my table:
<table>
<tr id="FromDateRow">
<td><asp:Label ID="FromDateLabel" runat="server">From date:</asp:Label></td>
<td>
<input type="text" id="txtFromDateF" class="needs-datepicker" />
<asp:TextBox id="txtFromDate" CssClass="hidden" runat="server" />
</td>
</tr>
<tr id="ToDateRow">
<td><asp:Label ID="ToDateLabel" runat="server">To date:</asp:Label></td>
<td>
<input type="text" id="txtToDateF" class="needs-datepicker" />
<asp:TextBox id="txtToDate" CssClass="hidden" runat="server" />
</td>
</tr>
<tr>
<td><asp:Label ID="CustomerCodeLabel" runat="server">Customer Code</asp:Label>:</td>
<td><asp:DropDownList ID="CustomerCodeDropDownList" runat="server" AutoPostBack="False" /></td>
</tr>
<tr>
<td><asp:Label ID="CINumberLabel" runat="server">CI Number</asp:Label>:</td>
<td><asp:TextBox ID="CINumberTextBox" runat="server" /></td>
</tr>
</table>
Now I want to do something like this:
Select Case value
Case DisplayDates.FromDate
ToDateRow.CssClass = "hidden"
FromDateRow.CssClass = ""
Case DisplayDates.ToAndFromDate
ToDateRow.CssClass = ""
FromDateRow.CssClass = ""
Case Else
ToDateRow.CssClass = "hidden"
FromDateRow.CssClass = "hidden"
End Select
For some reason I cannot access the ToDateRow and the FromDateRow from my codebehind.
The objects you are trying to reference in codebehind (the relevant tr elements) needs to be defined as runat="server"
If CssClass is not a known property for the object instance try using the following:
rowObject.Attributes.Add("class", "hidden");
You must have
runat="server
tag in table and tr to access from code behind.
After comment :
ToDateRow.Attributes("class") = "CssClass";
Ok I solved it.
By adding the runat="server" i was able to access the row.
And then I found the "visible"-property.
Now it works just fine.
Thx for the help
I have records from database separated by department, I want to tile them horizontally inside different tables (for each department). Here is what I tried (This does not work)
Aspx
<asp:ListView ID="lvUnderwriting" GroupItemCount="6" runat="server" GroupPlaceholderID="grpPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table>
<asp:PlaceHolder ID="grpPlaceHolder1" runat="server" ></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<span><b><%# Eval("gensubbusiclass") %></b></span>
<table>
<tr>
<th>Role Name:</th>
<th>Pending Tasks:</th>
<th>On Leave:</th>
</tr>
<tr>
<asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>
</tr>
</table>
</GroupTemplate>
<ItemTemplate>
<tr>
<td><span> <%#Eval("RoleName") %></span></td>
<td><span><%# Eval("Count") %></span></td>
<td><span><%# Eval("OnLeave") %></span></td>
</tr>
</ItemTemplate>
</asp:ListView>
Database rows
My current results. For some reason the second department is divided into two tables. Also how do I tile them horizontally?
Okay what I got was that ListView cannot display master detail records out of the box. Here is what I tried to get it working for me. The trick was to dynamically add a new row to the table if the header databound item was changed, the database records need to be sorted in advance to get this working. Referenced from here using Asp.net 3.5 ListView 4 Guys from Rolla
Aspx
<asp:ListView ID="lvUnderwriting" runat="server" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table border="1" cellpadding="4px">
<tr>
<th>Role Name:</th>
<th>Assigned:</th>
<th>On Leave:</th>
</tr>
<tr>
<asp:PlaceHolder ID="itemPlaceHolder1" runat="server" />
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<%# AddGroupUnderwriting() %>
<td><%# Eval("RoleName") %></td>
<td><%# Eval("Count") %></td>
<td><%# Eval("OnLeave") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
C#
// Add group row to underwriting grid
private string prevUWBusinessClass = "";
public string AddGroupUnderwriting()
{
string currentUWBusinessClass = Eval("gensubbusiclass").ToString();
if (currentUWBusinessClass != prevUWBusinessClass)
{
prevUWBusinessClass = currentUWBusinessClass;
return string.Format(#"<tr><td colspan=""3""><h3>Department: {0}</h3></td></tr>", currentUWBusinessClass);
}
else
return "";
}
I have a repeater. And i want to hide and display a particular column for a particular condition. I have three types of subjects and their ids are 0,1,2 respectively. Now i want to show that particular column when the subject will be 2 only..
My code is :-
<table id="table1" class="yui" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>
EmpID #
</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repaddressorbbl" runat="server" OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empid")%>
</td>
<td>
<asp:LinkButton ID="lknumber" runat="server" Text="Edit" CommandName="subjectid" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
</tfoot>
</table>
You could catch the OnItemDataBound event of the repeater and hide the column there if the (subject) item id is 2.
In order you can get a reference to the column, make it a server control:
<td style="text-align: center;" id="COL_TO_HIDE" runat="server"><%#Eval("empid")%></td>
Then in the repeater event you can simply look for the control and hide it:
protected void YourRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var subject = (Subject)e.Item.DataItem;
if (subject.Id == 2)
{
var col = e.Item.FindControl("COL_TO_HIDE");
col.Visible = false;
}
}
}
Please note, this is just a simplified example which should you get started.
I think you should start by using <HeaderTemplate></HeaderTemplate> and <FooterTemplate></FooterTemplate> to define the start and end of your table just to tidy it up.
You can get the table to run on the server by adding a runat="server" and give the column <td> an id and a runat="server" attribute so you can program server code against it. I'd then eval bind the visible attribute of the cell based on your field value or use attributes.add("display:none") or just use a grid view as suggested in the link.
<asp:Repeater ID="Repaddressorbbl" runat="server"
OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empid")%>
</td>
<% if (false){ %>
<td>
<asp:LinkButton ID="lknumber" runat="server"
Text="Edit" CommandName="subjectid" />
</td>
<% } %>
</tr>
</ItemTemplate>
</asp:Repeater>