如何创建使用 asp:ListItem 列表作为子控件的 ASP.NET 用户/服务器控件?
我希望创建一个用户/服务器控件,该控件将使用如下内容创建:
<my:MyListControl runat="server">
<asp:ListItem Text="Test1" Value="Test1" />
<asp:ListItem Text="Test2" Value="Test2" />
</my:MyListControl>
我只是在这里寻找一个开始: 文章或代码示例。
我应该继承哪个基类? 要覆盖什么?
可能如何自定义我的控件接受哪些子项(my:ListItem 而不是 asp:ListItem)。
我想要做的是为我网站的一小部分创建一个非常简单的面包屑控件。 我可以使用现有的 ASP.NET 控件,但这些项目是在代码中添加的,这意味着修复拼写错误或格式错误需要重新编译,这并不理想。
编辑:
这是我的代码,添加了以下 Josh 的建议:
命名空间 MySite.Controls 部分类面包屑 Inherits UserControl
Private m_BreadCrumbs As New List(Of BreadCrumbItem)
<PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property Items() As List(Of BreadCrumbItem)
Get
Return m_BreadCrumbs
End Get
Set(ByVal value As List(Of BreadCrumbItem))
m_BreadCrumbs = value
End Set
End Property
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Bind()
End Sub
Private Sub Bind()
lvCrumbs.DataSource = Items
Me.DataBind()
End Sub
End Class
Public Class BreadCrumbItem
Private m_Text As String
Public Property Text() As String
Get
Return m_Text
End Get
Set(ByVal value As String)
m_Text = value
End Set
End Property
Private m_Url As String
Public Property Url() As String
Get
Return m_Url
End Get
Set(ByVal value As String)
m_Url = value
End Set
End Property
End Class
End Namespace
那么我的页面代码如下所示:
<%@ Page Language="VB" AutoEventWireup="false" Inherits="MySite.MyPage" Title="My Page" Codebehind="MyPage.aspx.vb" %>
<%@ Register TagPrefix="my" Namespace="MySite.Controls" Assembly="MySite" %>
<my:BreadCrumbs ID="breadcrumbs" runat="server">
<Items>
<my:BreadCrumbItem Text="Another page" Url="AnotherPage.aspx" />
</Items>
</my:BreadCrumbs>
I am looking to create a user/server control that will be created with something like the following:
<my:MyListControl runat="server">
<asp:ListItem Text="Test1" Value="Test1" />
<asp:ListItem Text="Test2" Value="Test2" />
</my:MyListControl>
I am just looking for a start here:
Articles or code samples.
What base class should I inherit from? What to override?
Possibly how to customize what sub items my control accepts (my:ListItem instead of asp:ListItem).
What I am looking to do is create a very simple bread crumb control for a small section of my site. I have it all working with stock ASP.NET controls, but the items are added in code, which means fixing a spelling mistake or formatting bug involves a recompile, which is not ideal.
EDIT:
Here's my code with the addition of Josh's suggestion below:
Namespace MySite.Controls
Partial Class BreadCrumbs
Inherits UserControl
Private m_BreadCrumbs As New List(Of BreadCrumbItem)
<PersistenceMode(PersistenceMode.InnerProperty)> _
Public Property Items() As List(Of BreadCrumbItem)
Get
Return m_BreadCrumbs
End Get
Set(ByVal value As List(Of BreadCrumbItem))
m_BreadCrumbs = value
End Set
End Property
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Bind()
End Sub
Private Sub Bind()
lvCrumbs.DataSource = Items
Me.DataBind()
End Sub
End Class
Public Class BreadCrumbItem
Private m_Text As String
Public Property Text() As String
Get
Return m_Text
End Get
Set(ByVal value As String)
m_Text = value
End Set
End Property
Private m_Url As String
Public Property Url() As String
Get
Return m_Url
End Get
Set(ByVal value As String)
m_Url = value
End Set
End Property
End Class
End Namespace
Then my page code looks like this:
<%@ Page Language="VB" AutoEventWireup="false" Inherits="MySite.MyPage" Title="My Page" Codebehind="MyPage.aspx.vb" %>
<%@ Register TagPrefix="my" Namespace="MySite.Controls" Assembly="MySite" %>
<my:BreadCrumbs ID="breadcrumbs" runat="server">
<Items>
<my:BreadCrumbItem Text="Another page" Url="AnotherPage.aspx" />
</Items>
</my:BreadCrumbs>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在用户控件的代码后面添加一个属性,例如:
您的标记将是:
为了使 ListItem 可以成为您自己的列表项(我建议这样做,而不是使用 asp.net。)您需要创建你自己的班级。
这是我使用的服务器控件的示例(我删除了很多噪音,因此这只是一个骨架):
现在我可以像这样使用它:
You can add a property on a user control's code behind like:
Your markup would then be:
To make it so ListItem can be your own list item (Which I recommend doing as opposed to using asp.net.) You'll want to create your own class.
Here's an example of a Server Control I use (I removed a lot of the noise as such this is just a skeleton):
Now I can use this like:
使用 asp:Repeater,然后将其绑定到面包屑中所需的对象列表。 您可以使用模板自定义项目本身和分隔符。
Use an asp:Repeater then bind it to a List of the objects you want in your breadcrumb. You can use the template to customize the items themselves and the separators.