解析自定义控件的子级服务器控件的最简单方法. [英] Simplest way to parse child server controls of a custom control.

查看:71
本文介绍了解析自定义控件的子级服务器控件的最简单方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为Ad的UserControl,它代表一个广告.它使用属性和元素来填充其属性:

I have a UserControl called Ad that represents an advertisement. It uses attributes and elements to populate its properties:

<mabel:Ad runat="server" ID="Ad3" Weight="1">
    <Title>Fundraise with us!</Title>
    <Link>/info/fundraising+ideas</Link>
    <Image>fundraise.<ils:Query runat="server" />.jpg</Image>
    <Copy>Mabel's Labels' fundraisers are easy and profitable. Sign-up your school or organization.</Copy>
    <Action>Sign-Up Now</Action>
</mabel:Ad>



属性的设置工作得很好,因为我已经使用了适当的属性:



The setting of properties works well because I''ve used the appropriate attributes:

[ParseChildren(true)]
public partial class Ad : UserControl
{
    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    [Required]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string Title { get; set; }
    /// <summary>
    /// Gets or sets the image.
    /// </summary>
    /// <value>The image.</value>
    [Required]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string Image { get; set; }

...



我的问题是我想解析这些子元素内的服务器控件,例如< ils:Query/> < Image>中显示的控件< mabel:ad xmlns:mabel =#unknown">的元素.控制.

由于我正在使用[ParseChildren(true)],因此不会解析这些嵌套控件,因为这些元素正被解析为要分配给属性的值.

到目前为止,我想到的最简单的解决方案是将自定义ControlBuilder应用于此服务器控件,设置[ParseChildren(false)],然后让ControlBuilder使用反射来设置每个属性的值.解析每个InnerText之后.

必须有一种简单的方法..?



My problem is that I''d like to parse server controls inside these child elements, such as the <ils:Query/> control shown in the <Image> element of the <mabel:ad xmlns:mabel="#unknown"> control.

Since I''m using the [ParseChildren(true)], these nested controls are not parsed because the elements are being parse as values to be assigned to properties.

The simplest solution I''ve come up with so far is applying a custom ControlBuilder to this server control, setting [ParseChildren(false)] and having the ControlBuilder set the values of each of the properties using reflection; after parsing the InnerText of each.

There must be a simple way .. ?

推荐答案

使用内置集合有什么问题?

What''s wrong with using built-in collections?

Sub CustomControl_PreRender(control As UserControl) Handles CustomControl.PreRender
    Dim ctrl as UserControl
    For Each ctrl in UserControl.Controls
       If Ctrl.Invalidated == True Then
           Ctrl.Render()
       End If
    Next
End Sub


这篇关于解析自定义控件的子级服务器控件的最简单方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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