Ordered list using repeater in ASP.net - c#

The code below keeps giving me an error:
Repeater code in .ASPX file
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate><ol class="DescriptionRepeater"></HeaderTemplate>
<ItemTemplate>
<li>
<%= this %>
</li>
</ItemTemplate>
<FooterTemplate><ol/></FooterTemplate>
</asp:Repeater>
Code to populate the repeater in .ASPX.CS file
im using visual studio 2017 the code is causing the ordered list to be filled with the path to the .aspx file
List<string> lstDescription = new List<string>();
lstDescription.Add("this is the first description");
lstDescription.Add("this is the second description");
rptDescription.DataSource = lstDescription;
rptDescription.DataBind();

It is not a practical scenario. Anyhow, you just use Container.DataItem, since DataItem itself is a string. Please make sure you use binding syntax <%# %> instead of <%= %>.
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate>
<ol class="DescriptionRepeater">
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Container.DataItem %>
</li>
</ItemTemplate>
<FooterTemplate>
<ol />
</FooterTemplate>
</asp:Repeater

Related

Why can I not use Eval() in an if condition inside a ListView?

With a set up like this:
<asp:ListView runat="server" ID="lvArticles">
<LayoutTemplate>
<div runat="server" id="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<% if (Eval("Document") != null) { %>
<a href="/Documents/" + <%# Eval("Document.Id") %> + ".pdf">
<%# Eval("Document.Name") %>
</a>
<% } %>
</ItemTemplate>
</asp:ListView>
I get the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Why is databinding methods like Eval() not allowed in if conditions like that? Is the ListView not a databound control?
Because the # inside <%# Eval("value") %> is what tells the server to do the data binding.

Generate DataList at runtime containing a Div

I have a DataList code which generates Div at run-time. This code was suggested by someone in one of my previous question:
<asp:DataList runat="server" ID="NamesDL" RepeatColumns="2" RepeatDirection="Horizontal">
<ItemTemplate>
<div class="header">
<%# Eval("Category") %>
</div>
<div class="Details">
<%# Eval("CategoryDetails") %>
</div>
</ItemTemplate>
</asp:DataList>
This is limited to one DataList control. I want to generate several DataList controls at run-time and want to display on each tab page of the AJAX Tab container. I also want to generate the Div dynamically.
I have a loop and I have a a slight idea:
for (int i=0; i<=3; i++)
{
DataList DL = new DataList();
Controls ctl = new Control();
DL.Conrols.Add(ctl);
}
I am not following how to include the Div with each dynamically generated Div. The DataList will fetch the columns for the Div via a DataSource.
I want the code to be compatible with .NET 3.5.
I think you want a asp:Repeater.
<asp:Repeater id="NamesDL" runat="server">
<HeaderTemplate>
<div class="header">
<%# Eval("Category") %>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="Details">
<%# Eval("CategoryDetails") %>
</div>
</ItemTemplate>
<FooterTemplate>
<!--if you have other stuff, add it here-->
</FooterTemplate>
</asp:Repeater>
Then bind your DataList to the Repeater.

Using asp.net in html pages

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.

Hilighting the current page in an asp.net 3.5 master page

In my project the master page contains a repeater that's used as a menu with an Xml file as the data source for the repeater.
<asp:Repeater ID="Admin_menus" runat="server">
<HeaderTemplate><div id="navmenu"><ul></HeaderTemplate>
<FooterTemplate>|</ul></div></FooterTemplate>
<ItemTemplate>
|<li>
<a href="<%# DataBinder.Eval(Container.DataItem, "url")%>"
class="link6" id="<%# DataBinder.Eval(Container.DataItem, "id")%>">
<strong>
<%# DataBinder.Eval(Container.DataItem, "title")%>
</strong>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
urls in the xml file is as
<menuitems>
<item id="1" url="Employee.aspx" title="Employee" description="Employee" />
<item id="2" url="Location.aspx" title="Location" description="Location" />
</menuitems>
Here I want to change the CSS style of the current page in the menu.
One solution you can opt for is to handle the ItemCreated event of the <asp:Repeater> control. To do this you need to add an event handler:
In the .master markup:
<asp:Repeater ID="Admin_menus" runat="server" OnItemCreated="Admin_menus_ItemCreated">
<HeaderTemplate>
<div id="navmenu">
<ul>
</HeaderTemplate>
<FooterTemplate>
|</ul></div></FooterTemplate>
<ItemTemplate>
|<li runat="server" id="hyperlink"><a href="<%# DataBinder.Eval(Container.DataItem, "url")%>" class="link6" id="<%# DataBinder.Eval(Container.DataItem, "id")%>">
<strong>
<%# DataBinder.Eval(Container.DataItem, "title")%></strong> </a></li>
</ItemTemplate>
</asp:Repeater>
In the .master.cs codebehind:
protected void Admin_menus_ItemCreated(object sender, RepeaterItemEventArgs e)
{
// Ensure that the ItemCreated is not null, the first one (header?) gets
// returned null
if (e.Item.DataItem != null)
{
// Extract the "url" attribute from the Xml that's being used for
// databinding for this particular row, via casting it down to
// IXPathNavigable as the concrete type of e.Item.DataItem isn't available
// to us.
var currentUrl = ((IXPathNavigable)e.Item.DataItem).CreateNavigator().GetAttribute("url", "");
if (Request.Url.PathAndQuery.Contains(currentUrl))
{
// This just adds a background color of "red" to the selected
// menu item. What you actually do is entirely up to you
var hyperlink = (HtmlGenericControl) e.Item.FindControl("hyperlink");
hyperlink.Style.Add(HtmlTextWriterStyle.BackgroundColor, "red");
}
}
}
Note that I've added a runat="server" as well as an id="hyperlink" to the <li> tag in your ItemTemplate so that the code in the ItemCreated handler can find it easily to style it.
Perhaps one solution is to check the current page in your inline Eval code and add the "currentpage" class to the anchor
For simplicity I'm using Eval() instead of DataBinder.Eval()
<asp:Repeater ID="Admin_menus" runat="server">
<HeaderTemplate>
<div id="navmenu"><ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<%# Eval("url") %>' class='link6<%# Request.RawUrl.EndsWith(Eval("url").ToString()) ? " currentpage" : "" %>' id='<%# Eval("id")%>'><strong><%# Eval("title")%></strong></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></div>
</FooterTemplate>
</asp:Repeater>
Instead of binding directly from your xml data to repeater, try to get the repeater databing event, and compare with the current page url and the each binding item to determine if the current item should be hi-lighted

How to have an ASP checkbox for each entry in a list?

I want to do something like this in my asp code:
<%
foreach Record record in listOfRecords
{
%>
<asp:checkbox runat="server" id="employeeIdNumber" />
<p>Employee's Name: <%= record.name %> </p>
<p>Employee's Phone Number: <%= record.phoneNumber %></p>
<%
}
%>
The problem is that the checkbox id is a string literal. How can I give a unique id to each employee's checkbox?
Wrap this in a repeater. Then bind your listOfRecords to the repeater.
<asp:Repeater runat="server">
<ItemTemplate>
<asp:checkbox runat="server" id="employeeIdNumber" />
<p>Employee's Name: <%# Eval("name") %> </p>
<p>Employee's Phone Number: <%# Eval("phoneNumber") %></p>
</ItemTemplate>
</asp:Repeater>
Then to get it out, you run through the RepeaterItems collection and look for the checkboxes by RepeaterItem.FindControl("employeeIdNumber") to determine if they are checked.
I think you should use Repeater instead.
<asp:Repeater ID="rptEmployees" runat="server">
<ItemTemplate>
<asp:CheckBox ID="employeeIDNumber" runat="server" />
</ItemTemplate>
</asp:Repeater>
This Control will render unique ID for each checkbox for you.
CheckboxList is an option too.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx
This also has an example on how to check the checked state - just iterate through and check the Selected property.

Categories

Resources