I have a aspx page and want to generate the page dynamically using a DataTable using server tags.
My code looks like this:
<table style="width: 100%">
<% foreach (System.Data.DataRow row in UserReports.Rows) { %>
<tr>
<td>
<% if (row["ID"] != DBNull.Value) { %>
<div>
<table>
<tr>
<td>
<p><%=row["ReportTitle"]%></p>
</td>
<td>
<asp:Button ID="btnRunReport_<%=row["ID"]%>" runat="server" Text="Open" />
</td>
</tr>
</table>
</div>
<% } %>
</td>
</tr>
<% } %>
</table>
When I navigate to my page I get error:
Server tags cannot contain <% ... %> constructs
The error happens here and I tried different comination of using the server tag but could not find out the right way of assigning the ID to the dynamically creating button.
<asp:Button ID="btnRunReport_<%=row["ID"] %>"
If you want to use a DataBinding expression, you need to write is like this
//this does not work
<asp:Button ID='<%# "btnRunReport_" + row["ID"] %>' runat="server" Text="Open" />
//this does because you cannot dynamically set the ID but every other property
<asp:Button ID="Button1" CssClass='<%# "btnRunReport_" + row["ID"] %>' runat="server" Text="Open" />
But you have to call DataBind() manually in Page_Load for that to work.
But I would recommend you look into a GridView Control. It creates a <table> in html and you can have other Controls in the ItemTemplates
I'm currently trying to paginate a list of records, however; after the last table, the page break leaves an empty page. The set up is an Aspx files that incorporates two ascx files to draw the table records.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Table.ascx.cs" Inherits="%>
<br class="clear" /><br/>
<table class="report-table" cellspacing="0" cellpadding="2">
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("PatientBills") %>' OnItemDataBound="Repeater1_OnItemDataBound">
<HeaderTemplate>
<tr>
<td>Seq</td>
<td>Billing Id</td>
<td>Patient Id</td>
<td>Patient Name</td>
<td>Sex</td>
<td class="PrintNormalWrap">Admit Date</td>
<td class="PrintNormalWrap" style="width: 80px;">Social Security</td>
<td>DOB</td>
<td>POS</td>
<td>CPT</td>
<td>Mods</td>
<td style="width: 220px;">Diagnosis</td>
<td class="PrintNormalWrap">Referring Provider(UPIN)/ Comment</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="billRowShade<%# Container.ItemIndex % 2 %>">
<asp:TableCell ID="tableCellSequence" runat="server"><%# Container.ItemIndex + 1 %></asp:TableCell>
<td><%# FormatBillingId(Eval("Bill.BillingId")) %></td>
<td><%# FormatField(Eval("Patient.PatientId")) %></td>
<td id="printPatientName"><%# FormatField(Eval("Patient.LastName")) %>, <%# FormatField(Eval("Patient.FirstName")) %></td>
<td><%# FormatField(Eval("Patient.Sex")) %></td>
<td><%# FormatShortDate(Eval("Patient.DateOfAdmittance"))%></td>
<td><%# FormatField(Eval("Patient.SocialSecurityNumber")) %></td>
<td><%# FormatShortDate(Eval("Patient.DateOfBirth"))%></td>
<td><%# FormatField(Eval("Bill.Pos")) %></td>
<td><%# FormatField(Eval("Bill.CptCode")) %></td>
<td><%# FormatField(Eval("Bill.Mod")) %></td>
<td><%# FormatDiagnoses(Eval("Bill.Diagnoses"), Eval("Bill.BillDate"), Eval("Bill.SiteName")) %></td>
<td class="PrintNormalWrap"><%# FormatNotes(Eval("Patient.ReferringMd"), Eval("Patient.RefUpin"), Eval("Bill.Notes"), Eval("Bill.TimeEntry")) %> <%# FormatSupervisor( Eval("Bill.SupervisedByName"), Eval("Bill.DoctorName") ) %></td>
</tr>
<asp:TableRow ID="tableRowSpecialtyFieldDetails" runat="server" CssClass="specialtyFieldDetails">
<asp:TableCell ID="TableCell1" colspan="12" runat="server"><span id="spanSpecialtyFieldDetails" runat="server"></span></asp:TableCell>
</asp:TableRow>
</ItemTemplate>
</asp:Repeater>
</table>
<p style="page-break-after:always"></p>
<br/>
i tried and get the sum of two numbers by getting through eval
<%# Convert.ToDecimal(Eval("total"))+ Convert.ToDecimal(Eval("tax")) %>
by doing this i got the sum of (total + tax)
now i need to find the division. Is it possible to divide like <%# Convert.ToDecimal(Eval("total"))/ Convert.ToDecimal(Eval("qty")) %> the above.
my code in detail
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td class="text-left"><%#Eval("rowId") %></td>
<td><%#Eval("product_name") %> - <%#Eval("product_des") %></td>
<td class="text-center"><%#Eval("qty") %></td>
<td class="text-center"><%#Eval("uom") %></td>
<td class="text-center"><%#Eval("tax_percent") %></td>
<td class="text-right"><%# Convert.ToDouble(Convert.ToDecimal(Eval("total"))/ Convert.ToDecimal(Eval("qty")))%></td>
<td class="text-right"><%# Convert.ToDecimal(Eval("total"))+ Convert.ToDecimal(Eval("tax")) %></td>
<td class="text-center"><%#Eval("type") %></td>
<td class="text-right">
<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="assets/btn/edit_btn.png" OnClick="ibtnEdit_Click" CommandArgument='<%#Eval("rowId") %>' />
<asp:ImageButton ID="ibtn_Delete" runat="server" ImageUrl="assets/btn/delete_btn.png" OnClick="ibtn_Delete_Click" CommandArgument='<%#Eval("rowId") %>' OnClientClick="return confirm('Are you sure you want to delete?');" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
please help me. thanks in advance
In my ascx file I use a Repeater to create rows for the table. So basically I just repeat one row and populate the data into that:
<asp:Repeater ID="companyRepeater" runat="server">
<ItemTemplate>
<tr class = "DGItemStyle" id="myresultsRow1" runat="server">
<td style="padding:0.5em;" align="center"><%#Eval("compnSN1") %></td>
<td style="padding:0.5em;"><%#Eval("compnAddress1") %></td>
</tr>
</Itemtemplate>
</asp:Repeater>
But I need to change the style of the row every time, from:
CssClass="DGItemStyle"
to
CssClass="DGAlternatingItemStyle"
then again and again.
So what I tried is replace : class = "DGItemStyle" by : <%#Eval("compnStyle1")%> , and I also populate compnStyle1 by the string
CssClass="DGItemStyle" and CssClass="DGAlternatingItemStyle"
alternatively in my codebehind.
But looks like the tag doesn't accept this solution. It shows this error:
Parser Error Message: The server tag is not well formed.
So how could I archive this ?
Use Repeater.AlternatingItemTemplate
<asp:Repeater ID="companyRepeater" runat="server">
<AlternatingItemTemplate>
<tr class = "DGAlternatingItemStyle" id="myresultsRow1" runat="server">
<td style="padding:0.5em;" align="center"><%#Eval("compnSN1") %></td>
<td style="padding:0.5em;"><%#Eval("compnAddress1") %></td>
</tr>
</AlternatingItemTemplate>
<ItemTemplate>
<tr class = "DGItemStyle" id="myresultsRow1" runat="server">
<td style="padding:0.5em;" align="center"><%#Eval("compnSN1") %></td>
<td style="padding:0.5em;"><%#Eval("compnAddress1") %></td>
</tr>
</Itemtemplate>
</asp:Repeater>
Check this link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.alternatingitemtemplate%28v=vs.110%29.aspx
From MSDN
<AlternatingItemTemplate>
<tr>
<td style="background-color:Aqua">
<b><%# Container.DataItem %></b>
</td>
</tr>
</AlternatingItemTemplate>
<ItemTemplate>
<tr>
<td style="background-color:Silver">
<%# Container.DataItem %>
</td>
</tr>
</ItemTemplate>
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;
}
}