根据控件属性查找其子元素?

4

我希望为我的网站创建一个导航栏。该导航栏将包含指向不同页面的链接,且应突出显示用户当前所在页面的链接。

目前,我有如下html:

<div id="navbar" runat="server">
    <a href="/" runat="server" id="lnkHome">Home</a> |
    <a href="~/info.aspx" runat="server" id="lnkInfo">Info</a> |
    <a href="~/contacts.aspx" runat="server" id="lnkContacts">Contacts</a> |
    <a href="~/settings.aspx" runat="server" id="lnkSettings">Settings</a>
</div>

在我的PageLoad事件中编写以下代码:
//Show the currently selected page
String filename = System.IO.Path.GetFileNameWithoutExtension(Request.Path).ToLower();
if (filename == "default")
    lnkHome.Attributes.Add("class", "selected");
else if (filename == "info")
    lnkInfo.Attributes.Add("class", "selected");
else if (filename == "contacts")
    lnkContacts.Attributes.Add("class", "selected");
else if (filename == "settings")
    lnkSettings.Attributes.Add("class","selected");

这很难维护。如果我想添加一个链接,我必须为它指定一个id,并为它添加if语句的信息。我希望有一个更灵活的系统,可以动态地向导航栏添加链接,并在用户访问正确页面时突出显示。
我该怎么做?是否可以基于它们的href属性搜索navbar的子元素?最好这些元素不需要具有runat="server"属性,因此它们可以像普通HTML一样处理。或者我应该考虑不同的实现方式?
1个回答

1
我遇到了许多需要查找后代或祖先的情况。为此,我编写了一些扩展方法来帮助自己。我建议使用以下代码与这些方法:
所需的using语句:
using System.Collections.Generic;
using System.Web.UI;

扩展方法:

/// <summary>
/// Finds a single, strongly-typed descendant control by its ID.
/// </summary>
/// <typeparam name="T">The type of the descendant control to find.</typeparam>
/// <param name="control">The root control to start the search in.</param>
/// <param name="id">The ID of the control to find.</param>
/// <returns>Returns a control which matches the ID and type passed in.</returns>
public static T FindDescendantById<T>(this Control control, string id) where T : Control
{
    return FindDescendantByIdRecursive<T>(control, id);
}

/// <summary>
/// Recursive helper method which finds a single, strongly-typed descendant control by its ID.
/// </summary>
/// <typeparam name="T">The type of the descendant control to find.</typeparam>
/// <param name="root">The root control to start the search in.</param>
/// <param name="id">The ID of the control to find.</param>
/// <returns>Returns a control which matches the ID and type passed in.</returns>
private static T FindDescendantByIdRecursive<T>(this Control root, string id) where T : Control
{
    if (root is T && root.ID.ToLower() == id.ToLower())
    {
        return (T)root;
    }
    else
    {
        foreach (Control child in root.Controls)
        {
            T target = FindDescendantByIdRecursive<T>(child, id);
            if (target != null)
            {
                return target;
            }
        }

        return null;
    }
}

你的 C# 代码后端:

var fileName = Path.GetFileNameWithoutExtension(Request.Path);
var controlId = "lnk" + fileName;
var anchorTag = navbar.FindDescendantById<HtmlAnchor>(controlId);
anchorTag.Attributes.Add("class", "selected");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接