我有"TopHeader.ascx“(ViewUserControl)如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TopHeader.ascx.cs" Inherits="Sample.Web.CMS.Navigation.TopHeader" %>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<span class="flow-text selected-menu-option">
<%
var s = Model.FirstOrDefault(i => i.IsSelected);
s = s ?? Model[0];
%>
<%= s.Caption %>
<i class="glyphicon glyphicon-menu-right"></i>
</span>
</div>
</div>
</div>
public partial class TopHeader : ViewUserControl<List<MenuItem>>
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
public class MenuItem
{
public string Caption { get; set; }
public string Url { get; set; }
public bool IsSelected { get; set; }
}在_Layout.cshtml中,我有以下内容,它运行得非常好:
<body>
@Html.Partial("~/Navigation/TopHeader.ascx", new List<MenuItem>() { new MenuItem() { Caption = "Constituents", IsSelected = true}, new MenuItem() { Caption = "Fund Raising" }, new MenuItem() { Caption = "Orders" }, new MenuItem() { Caption = "Products" }, new MenuItem() { Caption = "Reporting" } })
</body>现在,我想在"TopHeader.ascx“中使用相同的"Site.Master”,定义如下:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Sample.Web.CMS.SiteMaster" %>代码隐藏
public partial class SiteMaster : MasterPage
{
...
}如何在"TopHeader.ascx“中使用"Site.master”。如何使用一些(菜单)数据实例化控件(正如我在MVC视图中所做的那样)?
发布于 2015-04-18 06:12:28
在Site.master中,除了两个更改之外,添加与_Layout.cshtml中的代码基本相同的代码:
<%: whatever %>而不是@whatever。因此,将母版页更改为:
public partial class SiteMaster : MasterPage
{
...
}并使用此代码呈现部分:
<%: Html.Partial("~/Navigation/TopHeader.ascx",
new List<MenuItem>() {
new MenuItem() { Caption = "Constituents", IsSelected = true },
new MenuItem() { Caption = "Fund Raising" },
new MenuItem() { Caption = "Orders" },
new MenuItem() { Caption = "Products" },
new MenuItem() { Caption = "Reporting" } })
%>https://stackoverflow.com/questions/29711397
复制相似问题