Hilighting the current page in an asp.net 3.5 master page - c#

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

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

Can OnItemCommand in Repeater redirect to a new tab?

I am trying to use the Repeater's OnItemCommand to open the aspx page in a new tab (preferably) or a new window. Can it be done with OnItemCommand?
Currently, it redirects to a new aspx page just fine but I stay in the same tab.
Here is my markup code:
<asp:Repeater ID="someRepeater" runat="server"OnItemCommand="Repeater_ItemCommand">
<HeaderTemplate>
//bluh bluh
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="link1" runat="server" CommandName="Redirect" CommandArgument='<%#Eval("textID") %>'><asp:Label Enable="true" ID="textID" runat="server" Text='<%#Eval("textName") %>'></asp:Label></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
//bluh bluh footer
</FooterTemplate>
</asp:Repeater>
Code Behind:
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e){
if (e.CommandName == "Redirect")
{
Response.Redirect("~/Other.aspx?id=" + e.CommandArgument, true);
}
}
Thank you,
I will post how I proceeded with my code. May be it will be of use to someone.
<asp:Repeater ID="someRepeater" runat="server">
<HeaderTemplate>
//bluh bluh
</HeaderTemplate>
<ItemTemplate>
<a href= "~/Other.aspx?id=<%#Eval('textID') %>" target="_blank" />
</ItemTemplate>
<FooterTemplate>
//bluh bluh footer
</FooterTemplate>
</asp:Repeater>
This seems to do the trick. I did not use the repeater's OnItemCommand. No code behind needed.
Thank you.

How to sort a ListView bound to an XML datasource

I want to sort the list view by a certain XML Element.
Here is my aspx code which shows how the listview is set up:
<asp:ListView ID="ListView1" runat="server" DataSourceID="newsDataSource">
<LayoutTemplate>
<div id="ItemPlaceHolderContainer" runat="server">
<span id="ItemPlaceHolder" runat="server" />
</div>
</LayoutTemplate>
<ItemTemplate>
<h2><%# XPath("title") %></h2>
<p><%# XPath("contents") %></p>
<p>Author: <%# XPath("author") %></p>
<p>Date Time: <%# XPath("datetime") %></p>
</ItemTemplate>
</asp:ListView>
<asp:XmlDataSource ID="myDataSource" runat="server" DataFile="~/Data.xml">
</asp:XmlDataSource>
Here is an example of the XML:
<?xml version="1.0" encoding="utf-8"?>
<theNews>
<news>
<title>Welcome!</title>
<contents>Hello, and welcome!</contents>
<author>Me</author>
<datetime>6/11/2014 4:40:35 PM</datetime>
</news>
</theNews>
What I want to do is sort the content in the ListView according to the 'datetime' XML element. Anyone know how I can do this?
protected void Page_Load(object sender, EventArgs e)
{
// TODO: Sort me here!!
}
You need to add this line into your Layout Template, either manually or add it to your code to populate it within the ItemPlaceHolder.
<asp:LinkButton runat="server" ID="SortByDateTime"
CommandName="Sort" Text="Date Time" CommandArgument="datetime"/>

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.

Get the current bounded object in a ListView's ItemTemplate

I want to be able to get the current bound object in the ItemTemplate of a ListView control.
Here's an example of what I want to do:
<asp:ListView ID="UserList" runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
//How can I get the current bound object in here?
</ItemTemplate>
</asp:ListView>
You can access it via the DataItem:
<%# DataBinder.Eval(Container.DataItem, "myPropertyName")%>'
If you wanted a textbox for example:
<asp:Label ID="MyProp" runat="server" Text='<%#Eval("myPropertyName") %>' />
If you just want the full object:
<%# (MyType)Container.DataItem %>

Categories

Resources