自定义用户控件替换的CheckBoxList TemplateControl? [英] Replace CheckBoxList TemplateControl with custom UserControl?

查看:139
本文介绍了自定义用户控件替换的CheckBoxList TemplateControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建标准CheckBoxList控件更详细的项目模板。它暴露了所谓的TemplateControl财产了Itemplate,但我没能找到如何实际使用它一个简单的资源。这里的code我到目前为止有:

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

这似乎是完全无视TemplateControl属性。任何想法?

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

推荐答案

的CheckBoxList的TemplateControl属性实际上并没有允许你修改的CheckBoxList的模板。这是继承System.Web.UI.Control一路属性,它意味着模板化控制的的CheckBoxList住在,或换句话说,在.aspx页面的.ascx用户控件或母版页,控制住在。 (如果控制是作为一个复合控件的一部分,我真的不知道没有尝试如果TemplateControl属性将返回null,或继续上涨的控制链,直到它找到了一个页面或用户控件)。

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.)

在CheckBoxList控件不提供你正在寻找做那种模板修改的。您可能需要定制绑定一个中继器或DataList控件(与在ItemTemplate CheckBox控件),以达到您想要的功能。

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.

这篇关于自定义用户控件替换的CheckBoxList TemplateControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆