Generate DataList at runtime containing a Div - c#

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.

Related

Ordered list using repeater in ASP.net

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

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.

Creating a repeater dynamically inside an accordion pane

My goal is to nest a Repeater inside an ASP.Net AJAX Accordion's AccordionPane.
So there is one Accordion which I am programmatically adding AccordionPanes to. The amount of panes I add depends on my particular dataset's count value, usually no more than 5. I've managed to do this successfully.
The thing I am having difficulty with is creating and adding a Repeater per each AccordionPane.
I've glanced over http://iridescence.no/post/Using-Templated-Controls-Programmatically.aspx but this is not exactly what I had in mind. Instead, I would rather declare a single Repeater as static HTML that I could then "clone" when I need. How can I achieve this? Obviously I would want each control's ID (within this declared Repeater) to be generated automatically each time i "clone" it.
The repeater looks like this:
<asp:Repeater ID="rptForum" runat="server">
<ItemTemplate>
<div runat="server" style="border:solid #d3d3d3 1px; border-bottom-width:0px;">
<table width="100%">
<tr><td align="left">
<asp:Label runat="server" Font-Size="12px" />
</td></tr>
<tr><td align="left">
>> <asp:Label runat="server" Font-Size="12px" Text='<%# Eval("query") %>' />
</td></tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
I might add more fields to be databound as I progress.
Any ideas appreciated..
You should be able to do that by adding the repeater to the Content template:
<cc1:AccordionPane ID="AccordionPane1" runat="server">
<Header>
Foo
</Header>
<Content>
<asp:Repeater ID="Repeater1" runat="server" ...>
...
</asp:Repeater>
</Content>
</cc1:AccordionPane>
You can do a hierarchical data bind with the accordion, as illustrated here: http://aspalliance.com/1674_complex_data_binding_with_the_accordion_control

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