Can I create custom directives in ASP.NET?

I created a menu control in asp.net and placed it on the main page. This menu control has the ActiveItem property, which, when changed, makes an item from the menu active.

To control the active element from a child page, I created a control that changes the property of the main page, introduced by the IMenuContainer interface, to update the active menu control on the main page.

public class MenuActiveItem : Control
{
    public ActiveItemEnum ActiveItem
    {
        get
        {
            var masterPage = Page.Master as IMenuContainer;
            if (masterPage == null)
                return ActiveItemEnum.None;

            return masterPage.ActiveItem;
        }

        set
        {
            var masterPage = Page.Master as IMenuContainer;
            if (masterPage == null)
                return;

            masterPage.ActiveItem = value;
        }
    }
}

Everything works fine, and I really like this solution, but I thought that if I knew, I would create a user directive with the same function instead of a user control, because it just matters more.

Does anyone know how to do this?

+3
1

, "".

, :

<%@ Page Language="C#" MasterPageFile="~/App.master"
CodeFileBaseClass="BasePage" ActiveItem="myActiveItem" AutoEventWireup="true"
CodeFile="Page1.aspx.cs" Inherits="Page1" %>

, . , .

+2

Source: https://habr.com/ru/post/1750712/


All Articles