3

我正在尝试为标准 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 属性。有任何想法吗?

4

2 回答 2

5

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.

于 2010-02-01T20:35:22.677 回答
1

你错过了几件事......这个 MSDN 页面有一个非常简单的例子:http: //msdn.microsoft.com/en-us/library/36574bf6.aspx

首先,您缺少 INamingContainer。

于 2010-02-01T20:19:22.387 回答