<asp:Repeater runat="server" ID="QuestionList">
<ItemTemplate>
<tr align="center">
<td><%# DataBinder.Eval(Container.DataItem, "ID") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Subject")%></td>
<td><abbr class="timeago" title="2008-07-17T09:24:17Z"><%# CommonFunctions.NiceDateTime(DataBinder.Eval(Container.DataItem, "DateSubmitted"))%></abbr></td>
<td></td>
<td><%# DataBinder.Eval(Container.DataItem, "Priority")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
My repeater works fine, but how can I format data that it uses by passing it through functions? For example the line:
CommonFunctions.NiceDateTime(DataBinder.Eval(Container.DataItem, "DateSubmitted"))
Doesn't work as container doesn't exist within current context.
It does. The following works inside an ItemTemplate for me:
<%# Server.HtmlEncode((Container.DataItem as YourFancyDataSetType).Description) %>
Of course, 'YourFancyDataSetType' could be any type. Simply convert/cast the current DataItem to the underlying type and give it to any function you may like.
#Edit: typos
First of all, I think you can use just <%# Eval("ID") %> as a shortcut for <%# DataBinder.Eval(Container.DataItem, "ID") %> and it could clean up your code a lot. There shouldn't be any problem passing the result of an Eval into a method, the Eval should get evaluated before being passed in. I believe it gets passed in as either an object or a string, though; it's been a few years since I've done that.
Related
I'm trying to understand why or how to code around a problem using an if statement buried in a .aspx page using a Boolean variable that is in the page behind.
The first method works fine if I keep all everything in simple strings:
<%# (startBlock ? "<tr><td>This works</td></tr>" : "") %>
However, the second method does not work.
<% if (startBlock)
{ %>
<tr><td>This does not work</td></tr>
<td>
<asp:Label ID="Label9" Text='<%# Eval("some_variable") %>' runat="server" /></td>
</td>
<%}%>
I understand that my variable startBlock needs to be bound but adding the # symbol causes an error. How can I accomplish the second method where I need to jump in and out of code that is in the code behind the .ASPX page?
Thank you in advance.
If I want to combine asp.net code with html (mark up) file, I need to open <% %> and execute teh code.
What If I want to iterate over a database with select query while feeding information and creating rows. For example:
<table>
<%
foreach(DataRow dr in dataset.Tables["empoloyees"].Rows)
{
%>
<tr>
<td>
<asp:Label runat="Server" Text="<% dr[FirstName].toString(); %>"/>
</td>
<td>
<asp:Label runat="Server" Text="<%dr[LastName].toString();%>"/>
</td>
</tr>
<%
}
%>
</table>
Is the syntax correct..and is that practice good (it is always used in php) ? or should I bind the data to the label somehow?(no idea how. but somehow)?
If you're trying to take a set of data and display it in a table then try using control like the GridView or the Repeater.
First and most important thing - do not mix business logic and data access functionality with the data representation markup!
Supposing you are using WebForms, you can use Repeater control which is bound in the code behind of a page/control (aspx.cs/ascx.cs) so View stay decoupled and just bound to specific properties of data source:
ASPX:
<asp:Repeater ID="employees" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label runat="Server"
Text="<%# DataBinder.Eval(Container.DataItem, "FirstName") %>"/>
</td>
<td>
<asp:Label runat="Server"
Text="<%# DataBinder.Eval(Container.DataItem, "LastName") %>"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Code Behind: (Page_Load() for instance)
employees.DataSource = dataset.Tables["empoloyees"].Rows;
employees.DataBind();
I suggest using a Repeater control if you want a piece of markup to iterate over and bind to it.
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "FirstName") %> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "LastName") %> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
Alternatively, use a GridView, though I find that Repeater gives you more control over the emitted markup.
You are almost there. Try this:
<table>
<%
foreach(DataRow dr in dataset.Tables["empoloyees"].Rows)
{
%>
<tr>
<td>
<%= dr[FirstName].toString(); %>
</td>
<td>
<%= dr[LastName].toString();%>
</td>
</tr>
<%
}
%>
</table>
You should use a repeater instead something like this
<table>
<asp:Repeater runat="server" ID="userRepeater" >
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"FirstName")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"LastName")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
and in your codebehind
userRepeater.DataSource = dataset.Tables["empoloyees"];
userRepeater.DataBind();
for a list of all types of <% %> take a look here
You don't have to use <% %> at all. Just put your code in between tags with runat="server", and then go ahead and use standard ASP.NET tags (e.g. asp:TextBox runat="server"). Just make sure it's an *.aspx page.
FWIW, your question seems a little backwards to me. If you are creating *.aspx pages then you can just use standard HTML anywhere you want. If you are trying to somehow squish ASP.NET into an *.html page - then you are misunderstanding how ASP.NET works.
In php you can have the serverside script print back a block of formatted code by using something like
print <<<HERE
<code>
<somemorecode></somemorecode>
</code>
HERE;
What is the asp equivalent (C#)??
It doesn't seem to like when I don't keep all of the string on one line and i'd like to avoid having to concatenate everything as it kind of kills the purpose of preserving the formatting for readability. Here's what I have that it isn't liking. i know why its doing it, but I'd like to know how to achieve what you can in php:
<%
for(int i = 20; i<21;i++){
Response.Write("
<tr>
<td class="style1">
<asp:DropDownList ID="docNumber" runat="server" />
</td>
<td class="style1">
<asp:Label ID="Label1" runat="server" Text="This would be the title of the document." /></td>
<td class="style1">#</td>
<td class="style1">
<asp:DropDownList ID="supervisorName" runat="server" />
</td>
</tr>");
%>
This isn't for anything anyone will ever see. I'm just trying to build a throw away data entry page for myself.
update: nevermind... i just realized this isn't going to work for me as it will duplicate each control id. fail.
At any rate, for future reference. is there a way to do what i was trying to do as far as the code blocks go?
You might consider using an <asp:Repeater /> for this purpose:
<asp:Repeater runat="server" id="repeater1">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="style1">
<asp:DropDownList ID="docNumber" runat="server" /></td>
<td class="style1">
<asp:Label ID="Label1" runat="server"
Text="<%# Container.DataItem("DocTitle") %>"/></td>
<td class="style1">#</td>
<td class="style1">
<asp:DropDownList ID="supervisorName" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In your code behind, you would then bind this to a list or datasource on page load (or where ever appropriate). Finally check out the ondataitembound action for custom handling of each item as it's bound.
MSDN Repeater - Everything you need to know
<%= Some String Expression Here %>
or
<% code with Response.Write("some string"); %>
the first is better if you want to print a single statement. The second is better if you have more than one expression that prints stuff (if you use loops, if/else, etc.)
Keep in mind that this is considered bad practice and is frowned upon in the ASP.NET Web Forms world. You should be using controls.
If you want to create some more complex layout that repeats something you should use databound controls. For tables use the GridView control, for list of options use DropDownList and for other things use layout use the ListView control (you can use it for tables too). These controls use default formatting or template for each item and are bound to a collection of items via code behind. All this is for ASP.NET Web Forms. If you are using ASP.NET MVC things are done differently.
For the solution, I cannot use any postback methods, because this is all working through ajax. The solution need to be implemented in the asp.net code.
I have a List<WebPage> that contains a list of Links (List<Link>) and I need for all the links to bind repetitive information such as page title, id, url. Here is my current repeater.
<div id="result">
<asp:Repeater runat="server" id="results">
<Itemtemplate>
<asp:Repeater runat="server" datasource='<%# Eval("Links") %>'>
<Itemtemplate>
<tr class="gradeX odd">
<td><%# Eval("Id") %></td> //property of WebPage (part of results repeater)
<td><%# Eval("Title") %></td> //property of WebPage (part of results repeater)
<td><%# Eval("Url") %></td> //property of WebPage (part of results repeater)
<td><%# Eval("URL") %></td>//Property of Link
<td><%# Eval("URLType") %></td> //Property of Link
<td><%# Eval("URLState") %></td> //Property of Link
</tr>
</Itemtemplate>
</asp:Repeater>
</Itemtemplate>
</asp:Repeater>
</div>
of course this doesnt work, how can i do this?
Thanks for your help!
Try this:
DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem,
"URL")
The key is to work your way back up to the parent repeater item, and then use the eval method.
Not of course actually.
I have almost the same, but into inner repeater datasource is set as DataSource='<%# GetLinks(Container.DataItem) %>'
where GetLinks returns casted enumerable of Links
I know how you can set an tag's url attribute programmatically in c#, but it seems when I try to access the image element inside of a tag I cannot access it.
The is residing in the <AlternatingItemTemplate>.
NOTE: I am only having this issue inside the <AlternatingItemTemplate>
Now the ListView tag is also databound.(this is probably why I cannot access, because it isn't guaranteed that it will even exist perhaps).
How can I get around this so that I can display my images programmatically or is there a better solution?
Here's the source:
<asp:ListView ID="ListView_Comments" runat="server"
DataKeyNames="ReviewID,ProductID,Rating" DataSourceID="EDS_CommentsList">
<ItemTemplate>
<tr style="background-color:#EDECB3;color: #000000;"> <td><%# Eval("CustomerName") %></td>
<td> <img src='Styles/Images/ReviewRating_d<%# Eval("Rating") %>.gif' alt="">
<br />
</td>
<td> <%# Eval("Comments") %>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#F8F8F8;"> <td><%# Eval("CustomerName") %></td>
<td>
<img id="rateImage" src="" alt="" runat="server" />
......
Should be able to access it in the codebehind in the OnItemDatabound Event. There u can check to see if its an alternating item or not then use FindControl and u should have access to it.
Or, if your image is stored in your datasource y not just set the src using <%# Eval %>