何时在母版页上加载默认内容
我的主母版页中有以下占位符。我不想在多个内容页面中重复我的新闻内容,因此我选择不在其中提供此内容,并且母版页应提供其默认值。
<asp:ContentPlaceHolder ID="SideBarContent" runat="server">
<asp:GridView ID="newsGrid" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:ContentPlaceHolder>
当我尝试以下代码时,出现错误,因为 newsGrid 为空。我认为我在页面生命周期中的错误位置执行此操作,但我不知道正确的位置在哪里。
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
if (!IsPostBack)
{
newsGrid.DataSource = _newsService.ListActive();
newsGrid.DataBind();
}
}
I have the following placeholder in my main master page. I would like to not have to duplicate my news content in several content pages, so I choose to simply not provide this content in them, and the master page should provide its default.
<asp:ContentPlaceHolder ID="SideBarContent" runat="server">
<asp:GridView ID="newsGrid" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:ContentPlaceHolder>
When I try the following code, I get an error because newsGrid is null. I assume I'm doing this at the wrong place in the page lifecycle, but I don't know where the right place is.
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
if (!IsPostBack)
{
newsGrid.DataSource = _newsService.ListActive();
newsGrid.DataBind();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ContentPlaceHolder 中的内容将被内容页提供的内容替换。
因此,当您的内容页有一个针对 SideBarContent 占位符的内容控件时,您的 newsGrid 将被替换。
考虑到这一点,您是在所有页面上还是仅在替换内容的页面上看到此问题?我希望对于替换内容页面中的内容的任何页面,该值都为空。
更新
我刚刚快速尝试过这个。
对于未定义面向
asp:ContentPlaceHolder
的asp:Content
控件的页面,asp:ContentPlaceHolder
内的控件将可用在后面的代码中给你。在它确实定义了一个针对此
asp:ContentPlaceHolder
的asp:Content
的页面中,则该asp:ConentPlaceHolder
内的控件将被删除,因此, 在后面的代码中访问时为 null。因此,当内容页提供内容时,母版页上占位符内的内容将被替换,因此您的母版页必须进行编码以应对这种情况。
因此,如果您在 Site.master 中有这个:
... 那么在 Page1.aspx 中您有这个...
... 那么该页面的按钮“foo”将为空。如果您省略此内容控件是 Page2.aspx,则按钮 foo 将被实例化并在后面的母版页代码中可用。
因此,我怀疑您只需要防止此网格为空,如果是,您可以假设内容页面提供了它自己的新闻列表。
The content between within your ContentPlaceHolder will be replaced by the content provided by the content page.
So, when your content page has a Content control that targets the SideBarContent placeholder your newsGrid will be replaced.
With this in mind, are you seeing this issue on all pages, or just pages that replace the content? I would expect this to be null for any page where you replace the content in a content page.
update
I've just quickly tried this.
With a page that doesn't define an
asp:Content
control that targets theasp:ContentPlaceHolder
the controls inside thatasp:ContentPlaceHolder
will be available to you in the code behind.In a page where it DOES define an
asp:Content
targetting thisasp:ContentPlaceHolder
then the controls inside thatasp:ConentPlaceHolder
are removed and, therefore, null when accessed in the code behind.So, this content within the placeholder on the masterpage will be replaced when a content page provides content, and so your master page must be coded to expect this situation.
So, if you have this in the Site.master:
... then in Page1.aspx you have this...
... then the button "foo" will be null for that page. If, you omit this Content control is Page2.aspx, the button foo will be instantiated and available in your masterpage code behind.
So, I suspect you simply need to guard against this grid being null and if it is you can assume the content page has provided it's own news list.