简体   繁体   English

自定义服务器控件:继承自Menu类,呈现方式以及呈现方式

[英]Custom Server Control: Inherited from Menu class, how and exactly what to render

I built a custom control for all my applications that would render menu taking name of the respective application and logged in user roles and render the menus accordingly. 我为所有应用程序构建了一个自定义控件,该控件将使用相应应用程序的名称来呈现菜单,并登录用户角色并相应地呈现菜单。 I am using an already written javascript for rendering menu that is loaded from db in the form of html string containing divs and unordered lists. 我正在使用一个已编写的javascript渲染菜单,该菜单从db以包含div和无序列表的html字符串的形式从数据库加载。 Now, coming to the original problem, previously I inherited my control with Panel class, which is rendered as div in html, I just wrote the generated HTML in the inherited panel container. 现在,要解决原始问题,以前我用Panel类继承了控件,该类在html中呈现为div,我只是将生成的HTML编写在继承的panel容器中。 Now, I want to integrate the control with the websites that already have sitemaps. 现在,我想将控件与已经具有站点地图的网站集成在一起。 For that, I inherited my control from Menu class and now I am confused how to render the menu html. 为此,我从Menu类继承了控件,现在我很困惑如何呈现html菜单。 The code of how I was rendering it previously is pasted below. 下面粘贴了我以前如何渲染的代码。

[DefaultProperty("Text")]
[ToolboxData(@"<{0}:MenuControl runat=""server"" \>")]
public class MenuControl : Panel
{ 
.
.
.
.
.
 #region Loading Resources On PreRender
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (string.IsNullOrEmpty(this.DataSourceID))
        {
            ClientScriptManager cs = this.Page.ClientScript;

            #region Loading JavaScript File(s)

            #endregion

            #region Loading CSS File(s)

            #endregion

            #region Loading Image(s)

            #endregion
        }
    }
    #endregion

    #region Rendering Final Contents
    protected override void RenderContents(HtmlTextWriter output)
    {
        string Menus = string.Empty;
        //Where as Menus contain menu in an html structure of ordered list.
        Menus = GetMenuForAppOnUser();

        output.Write(Menus);
    }
    #endregion
.
.   
.
.
}

Now, as it was inherited with Panel, so I just had to write some html inside the inherited panel(div). 现在,由于它是与Panel一起继承的,所以我只需要在继承的panel(div)内编写一些html。 How do I now write menuhtml when it is inherit from Menu class. 现在,当MenuMenu继承自Menu类时,我该怎么写。

Found out the solution, for Menu, Render actually renders the control instead of RenderContents as for Panel. 找到解决方案后,对于Menu, Render实际上渲染控件,而不是面板渲染 RenderContents

#region Rendering Final Contents
protected override void Render(HtmlTextWriter output)
{
    string Menus = string.Empty;
    //Where as Menus contain menu in an html structure of ordered list.
    Menus = GetMenuForAppOnUser();

    output.Write(Menus);
}
#endregion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM