How to achieve embedded custom tags in asp.net?

孤人 提交于 2020-01-17 07:30:50

问题


I found this example:

<telerik:RadDatePicker
 ID="RadDatePicker1"
 runat="server">
 <DateInput Width="100%"></DateInput>
 <Calendar
     CellAlign="Center"
     CellVAlign="Middle"
     DayNameFormat="FirstLetter"
     FirstDayOfWeek="Default"
     MonthLayout="Layout_7columns_x_6rows"
     Orientation="RenderInRows"
     TitleAlign="Center"
     UseColumnHeadersAsSelectors="False"
     ShowRowHeaders="False">
</Calendar>
<DatePopupButton 
     CssClass="radPopupImage_Default" 
     BorderColor="#D0E1F2" 
     BorderStyle="Solid" 
     BorderWidth="1px" />

My assumption is that inside the RadDatePicker there is a DateInput object, Calendar Object and DatePopupButton object.

I would like to have my own custom control that allows access to an inner object e.g.

    <jonno:textbox id="txt1" runat="server"><FieldConfig fieldName="Input1"/></jonno:textbox>

Ideally I don't want the FieldConfig class to be a visual class but it's ok if it is.

How can I achieve this?


回答1:


The embedded custom tags are properties of your control. To enable setting them in markup, you need to decorate your control and properties with the following attributes:

  • Control: ParseChilden, PersistChildren
  • Properties: PersistenceMode

Example from a control I use that does something similar:

/// <summary>
/// Control that will conditionally show one of two views
/// </summary>
[ParseChildren(true)]
[PersistChildren(true)]
public class EditingView : CompositeControl
{
    #region private fields

    private View _displayView = new View();
    private View _editView = new View();

    #endregion
    #region properties

    /// <summary>
    /// The view that will be rendered in display mode
    /// </summary>
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public View DisplayView
    {
        get
        {
            return _displayView;
        }
        set
        {
            _displayView = value;
        }
    }

    /// <summary>
    /// The view that will be rendered in editing mode
    /// </summary>
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public View EditView
    {
        get
        {
            return _editView;
        }
        set
        {
            _editView = value;
        }
    }
    /* Implementation details hidden */
}

Look the attributes up on msdn to read up on what they do exactly. The above should do what you need it to do though. In markup I can then simply assign the two views:

<ctl:EditingView runat="server">
<DisplayView>
    blah blah
</DisplayView>
<EditView>
    blah blah edit
</EditView>
</ctl:EditingView>

THe only difference is that my properties are still WebControls and take more child controls. It shouldn´t matter though, as long as you set your attributes right.

Menno



来源:https://stackoverflow.com/questions/7527573/how-to-achieve-embedded-custom-tags-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!