adding user control to default.aspx

Topics: Controls
Apr 30, 2012 at 10:40 AM

Hi

I am trying to ad my own user control to the default.aspx page, but for some reason I always get an error message coming for the PostViewBase class in the 
on Init fuction

 protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!this.Post.IsVisible)
            {
                this.Visible = false;
            }
        }

 telling me that the Post is null.

I have checked the IntelliTrace in visual studio and saw that the function SelectPost is called in the DbBlogProvider class, which is not usually called when I start the application.

even if my user control is empty, i still get this error message.

Coordinator
Apr 30, 2012 at 12:02 PM

That's strange.  I created a test user control and added it to default.aspx.  It's working for me.  Here's what I have for reference.  The user control, just one file (uc.ascx) in the root folder:

<%@ Control Language="C#" %>

<script runat="server">

	protected override void OnLoad(EventArgs e)
	{
		litTest.Text = "aaa";
	}

</script>

test <asp:Literal ID="litTest" runat="server" />

And then my modified default.aspx.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" %>
<%@ Register Src="User controls/PostList.ascx" TagName="PostList" TagPrefix="uc1" %>
<%@ Register Src="~/uc.ascx" TagName="TestControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server">
  <div id="divError" runat="Server" />
  <uc1:PostList ID="PostList1" runat="server" />
  <uc1:TestControl ID="testControl" runat="server" />
  <blog:PostCalendar runat="server" ID="calendar" 
    EnableViewState="false"
    ShowPostTitles="true" 
    BorderWidth="0"
    NextPrevStyle-CssClass="header"
    CssClass="calendar" 
    WeekendDayStyle-CssClass="weekend" 
    OtherMonthDayStyle-CssClass="other" 
    UseAccessibleHeader="true" 
    Visible="false" 
    Width="100%" />    
</asp:Content>
May 1, 2012 at 6:11 AM

thanks

I figue out what was the issue, I have set one of my user controls to inherit from PostView class.