【问题标题】:Design time issues with Composite Control with Devexpress control使用 Devexpress 控件的复合控件的设计时间问题
【发布时间】:2013-07-01 09:20:37
【问题描述】:

我正在创建一个复合控件类,其中包含多个 devexpress aspx 控件。但是,设计时存在一个问题:每当我更改我公开的子控件属性时,子控件都会丢失其界面/皮肤(如下图所示) 上图显示,leftlistbox,底部两个按钮是aspx控件,而rightlistbox是asp控件。每当我访问并更改复合控件的子控件属性时,只有那些 devexpress 控件会在设计时丢失其界面/外观,如上所示。 下面是我的复合控件类文件。

   [ParseChildren(true), PersistChildren(false)]
public class CompositeControl_TEST : CompositeControl, INamingContainer 
{
    private ASPxListBox leftListBox;
    private ListBox rightListBox;
    private ASPxButton lefttoright;
    private ASPxButton righttoleft;

    [Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), RefreshProperties(RefreshProperties.Repaint), NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public DevExpress.Web.ASPxEditors.ASPxListBox XF_leftListBox
    {
        get
        {
            this.EnsureChildControls();
            return leftListBox;
        }
    }

    [Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), RefreshProperties(RefreshProperties.Repaint), NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ListBox XF_rightlistBox
    {
        get
        {
            this.EnsureChildControls();
            return rightListBox;
        }
    }

    [Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), RefreshProperties(RefreshProperties.Repaint), NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public DevExpress.Web.ASPxEditors.ASPxButton XF_left2rightButton
    {
        get
        {
            this.EnsureChildControls();
            return lefttoright;
        }
    }

    [Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), RefreshProperties(RefreshProperties.Repaint), NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public DevExpress.Web.ASPxEditors.ASPxButton XF_right2leftButton
    {
        get
        {

            this.EnsureChildControls();
            return righttoleft;
        }
    }

    protected override void RecreateChildControls()
    {    
        base.EnsureChildControls();
    }

    protected override void CreateChildControls()
    {
        this.Controls.Add(new LiteralControl(@"<div>"));
        this.Controls.Add(new LiteralControl(@"<table>"));
        this.Controls.Add(new LiteralControl(@"<tr>"));

        this.Controls.Add(new LiteralControl(@"<td>"));
        this.leftListBox = new ASPxListBox();
        this.leftListBox.ID = this.ClientID + "leftListBox";
        this.leftListBox.Items.Add("left from cs");
        this.Controls.Add(leftListBox);
        this.Controls.Add(new LiteralControl(@"</td>"));

        this.Controls.Add(new LiteralControl(@"<td>"));
        this.rightListBox = new ListBox();
        this.rightListBox.ID = this.ClientID + "rightListBox";
        //this.rightListBox.Items.Add("Right from cs");
        this.Controls.Add(rightListBox);
        this.Controls.Add(new LiteralControl(@"</td>"));
        this.Controls.Add(new LiteralControl(@"</tr>"));

        this.Controls.Add(new LiteralControl(@"<tr>"));
        this.Controls.Add(new LiteralControl(@"<td>"));
        this.lefttoright = new ASPxButton();
        lefttoright.ID = this.ClientID + "lefttoright";
        lefttoright.Text = "lefttoright";
        this.Controls.Add(lefttoright);
        this.Controls.Add(new LiteralControl(@"</td>"));

        this.Controls.Add(new LiteralControl(@"<td>"));
        this.righttoleft = new ASPxButton();
        righttoleft.ID = this.ClientID + "righttoleft";
        righttoleft.Text = "righttoleft";
        this.Controls.Add(righttoleft);
        this.Controls.Add(new LiteralControl(@"</td>"));
        this.Controls.Add(new LiteralControl(@"</tr>"));
        this.Controls.Add(new LiteralControl(@"</div>"));
        base.CreateChildControls();
    }
}

}

我一直在网上搜索类似的问题,但似乎没有找到任何匹配的问题。

【问题讨论】:

    标签: c# asp.net devexpress composite-controls


    【解决方案1】:

    我知道这很旧,但我想我会插话的。 您必须重写设计器类 CompositeControlDesigner 作为默认调用 RecreateChildControls - 由于控件属性未保存在 viewState 中,因此在此方法调用期间会重新创建它们

    [Bindable(true),   DesignerSerializationVisibility(DesignerSerializationVisibility.Content), RefreshProperties(RefreshProperties.Repaint), NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public DevExpress.Web.ASPxEditors.ASPxButton XF_right2leftButton
    {
        get
        {
    
            this.EnsureChildControls();
            //this is not in viewsate and it is ill adviced to try and put it there. 
            return righttoleft;
        }
    

    }

    尝试创建派生自 CompositeControlDesigner 的设计器 然后像这样分配给您的自定义控件

    [
    Designer("YourDesignerNamespace.YourCompositeControlDesigner, " + YourAssemblyInfo)
    ] 
    [ParseChildren(true), PersistChildren(false)]
    public class CompositeControl_TEST : CompositeControl, INamingContainer 
    {....
    
    }
    

    像这样覆盖 CompositeControlDesigner 中的 CreateChildControls

    protected override void CreateChildControls()
        {
    
        }
    

    这将删除通过接口属性对 RecreateChildControl 的调用

    或者,您可以覆盖 GetDesignTimeHtml - 如果您不打算调用 base.GetDesignTimeHtml(此方法调用 CreateChildControls),这显然是可行的。这必须处理 Html(您只需在 ViewControl 上调用 RenderControl 即可);

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您没有覆盖设计器,它处理控件的设计时修改。 由于您要覆盖主要功能,因此可以保证控件仍然正常运行。或许,通过 WebUserControl 组织这样一个复杂的布局会更好...

      【讨论】:

      • 感谢您的 cmets,实际上我是使用 webusercontrol 来开发控件的,它似乎有一些限制......所以我尝试使用复合控件..你能解释一下如何实际覆盖设计器或发送我有什么文章可以做吗?
      猜你喜欢
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多