将 CheckBoxList TemplateControl 替换为自定义 UserControl?

发布于 2024-08-19 13:25:06 字数 839 浏览 6 评论 0原文

我正在尝试为标准 CheckBoxList 控件创建更详细的项目模板。它公开了一个名为 TemplateControl 的 ITemplate 属性,但我无法找到有关如何实际使用它的简单资源。这是我到目前为止的代码:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    Dim items As New List(Of ListItem)
    items.Add(New ListItem() With {.Text = "A", .Value = "1"})
    items.Add(New ListItem() With {.Text = "B", .Value = "2"})
    items.Add(New ListItem() With {.Text = "C", .Value = "3"})

    Dim lst As New CheckBoxList()
    Dim tpl As ITemplate = LoadTemplate("~/CustomListItem.ascx")
    Dim g As New TemplateControlWrapper()
    tpl.InstantiateIn(g)
    lst.TemplateControl = g
    lst.DataSource = items
    lst.DataBind()

    Form.Controls.Add(lst)

End Sub

Class TemplateControlWrapper
    Inherits UserControl

End Class

它似乎完全忽略了 TemplateControl 属性。有什么想法吗?

I am trying to create a more detailed item template for the standard CheckBoxList control. It exposes an ITemplate property called TemplateControl but I wasn't able to find a straightforward resource on how to actually use it. Here's the code I have so far:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    Dim items As New List(Of ListItem)
    items.Add(New ListItem() With {.Text = "A", .Value = "1"})
    items.Add(New ListItem() With {.Text = "B", .Value = "2"})
    items.Add(New ListItem() With {.Text = "C", .Value = "3"})

    Dim lst As New CheckBoxList()
    Dim tpl As ITemplate = LoadTemplate("~/CustomListItem.ascx")
    Dim g As New TemplateControlWrapper()
    tpl.InstantiateIn(g)
    lst.TemplateControl = g
    lst.DataSource = items
    lst.DataBind()

    Form.Controls.Add(lst)

End Sub

Class TemplateControlWrapper
    Inherits UserControl

End Class

It seems to be ignoring the TemplateControl property completely. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2024-08-26 13:25:06

CheckBoxList 的 TemplateControl 属性实际上不允许您修改 CheckBoxList 的模板。这是从 System.Web.UI.Control 继承的属性,它表示 CheckBoxList 所在的模板化控件,或者换句话说,该控件所在的 .aspx 页面、.ascx 用户控件或母版页继续生活。 (如果该控件作为复合控件的一部分包含在内,老实说,如果不进行实验,我不知道 TemplateControl 属性是否会返回 null,或者继续沿着控件链向上查找,直到找到 Page 或 UserControl。)

CheckBoxList 控件不会提供您想要进行的模板修改类型。您可能需要自定义绑定 Repeater 或 DataList(使用 ItemTemplate 中的 CheckBox 控件)才能实现您正在寻找的功能。

CheckBoxList's TemplateControl property does not actually allow you to modify the template of the CheckBoxList. This is a property inherited all the way from System.Web.UI.Control, and it means the templated control that the CheckBoxList lives in, or put another way, the .aspx page, .ascx user control, or master page that the control lives on. (If the control is included as part of a composite control, I honestly don't know without experimenting if the TemplateControl property would return null, or keep going up the control chain until it found a Page or UserControl.)

The CheckBoxList control does not offer the kind of template modification you are looking to do. You may need to custom-bind a Repeater or DataList (with a CheckBox control in the ItemTemplate) in order to achieve the functionality you're looking for.

离旧人 2024-08-26 13:25:06

你错过了一些东西......这个 MSDN 页面有一个非常简单的例子:
http://msdn.microsoft.com/en-us/library/36574bf6。 对于初学者来说

,您缺少 INamingContainer。

You are missing a couple things... this MSDN page has a pretty straightforward example:
http://msdn.microsoft.com/en-us/library/36574bf6.aspx

For starters, you are missing INamingContainer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文