English 中文(简体)
具有自定义列表项的.NET用户/服务器控件
原标题:.NET User/server Control With Custom List Items

我正试图创建一个简单的菜单用户控件,正如此处

附加的代码导致了一个“对象引用未设置为对象实例”的错误,但我不知道为什么。有什么想法吗?

<%@ Master Language="VB" CodeFile="MySite.master.vb" Inherits="MySite" %>
<%@ Register src="Controls/Menu.ascx" tagname="Menu" tagprefix="my"  %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MySite</title>
    <link href="Styles/MySite.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder id="headContent" runat="server">
    </asp:ContentPlaceHolder>    
</head>
<body id="masterBody" runat="server">
    <form id="form1" runat="server">
        <my:Menu ID="Menu1" runat="server">
            <MenuItems>
                <my:MenuItem Text="Test" NavigateUrl="~/Default.aspx" />
            </MenuItems>
         </my:Menu>
    </form>
</body>
</html>

Partial Class Controls_Menu
        Inherits System.Web.UI.UserControl

    Private m_Items As List(Of MenuItem) = Nothing
        <PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property MenuItems() As List(Of MenuItem)
            Get
                Return m_Items
            End Get
            Set(ByVal value As List(Of MenuItem))
                m_Items = value
            End Set
        End Property

    End Class

    Public Class MenuItem
        Private m_Text As String
        Public Property Text() As String
            Get
                Return m_Text
            End Get
            Set(ByVal value As String)

            End Set
        End Property
        Private m_NavigateUrl As String
        Public Property NavigateUrl() As String
            Get
                Return m_NavigateUrl
            End Get
            Set(ByVal value As String)
                m_NavigateUrl = value
            End Set
        End Property
    End Class
问题回答

问题在于:

<MenuItems>
     <my:MenuItem Text="Test" NavigateUrl="~/Default.aspx" />
</MenuItems>

ASP.net正在尝试添加到您的MenuItems列表中,它通过调用

MenuItems.Add(...)

但是,由于m_Items等于Nothing,因此会出现错误。修复此替换

Private m_Items As List(Of MenuItem) = Nothing

具有

Private m_Items As List(Of MenuItem) = New List(Of MenuItem)()




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签