如何分配动态CSS类的HyperLink位于里面的ListView [英] How to assign dynamic css class to HyperLink located inside ListView
问题描述
有点儿混乱的称号。
这是我的老导航
<li><a href="General.aspx" runat="server" id="currentGeneral"><i class="home"></i> Overview</a></li>
第i类设置旁边的导航标签的图标。
The i class sets an icon next to the navigation tab.
在Site.Master.CS我查了一下当前的页面,并将其设置为主动与下面的code。
On the Site.Master.CS I checked what the current page was and would set it to active with the code below.
currentGeneral.Attributes["class"] = "active";
于是我改变了导航由数据库填充一个ListView。
So I changed the navigation to a listview populated by a database.
<asp:ListView ID="ListViewMenu" runat="server" ItemPlaceholderID="menuContainer">
<LayoutTemplate>
<ul class="menu" id="responsive" runat="server">
<asp:PlaceHolder ID="menuContainer" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><a href='<%#Eval ("href") %>' class='<%#Eval ("id") %>'> <i class='<%#Eval ("class") %>'></i><%#Eval ("text") %></a></li>
</ItemTemplate>
</asp:ListView>
但现在,我使用列表视图中,currentGeneral ID不存在,我不能将它设置为活动的。
But now that I am using listview, the currentGeneral id does not exist and I cant set it to active.
我试图想什么是最好的方式来得到这个工作的。任何人有一个建议?
I was trying to think what the best way to get this to work is. Anyone have a suggestion?
感谢您。
推荐答案
如果您要访问 ListView控件中的各个项目
,同时结合中,可以使用需要不同的方法的ItemDataBound 事件。
If you want to access individual item inside of ListView
while binding, you might want a different approach using ItemDataBound event.
请务必投给DataItem的对象appropiate。例如, DataRowView的
Please make sure to cast DataItem to appropiate object. For example, DataRowView
<asp:ListView ID="ListViewMenu" runat="server"
OnItemDataBound="ListViewMenu_ItemDataBound"
ItemPlaceholderID="menuContainer">
<LayoutTemplate>
<ul class="menu" id="responsive" runat="server">
<asp:PlaceHolder ID="menuContainer" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" ID="HyperLink1" >
<i class='<%#Eval ("class") %>'></i><%#Eval ("text") %>
</asp:HyperLink>
</li>
</ItemTemplate>
</asp:ListView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateMenu();
}
}
protected void ListViewMenu_ItemDataBound(
object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var rowView = e.Item.DataItem as DataRowView;
var hyperLink = e.Item.FindControl("HyperLink1") as HyperLink;
hyperLink.NavigateUrl = rowView["href"].ToString();
hyperLink.CssClass = rowView["menu"].ToString();
if (Request.Path.ToLower().Contains(rowView["href"].ToString()))
hyperLink.CssClass += " active";
}
}
void PopulateMenu()
{
DataAccess da = new DataAccess();
da.AddParameter("ID", ID, DataAccess.SQLDataType.SQLInteger, 4);
SiteMenu = da.runSPDataSet("Portal_MenuCreate");
ListViewMenu.DataSource = SiteMenu;
ListViewMenu.DataBind();
}
这篇关于如何分配动态CSS类的HyperLink位于里面的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!