代码之家  ›  专栏  ›  技术社区  ›  djdd87

如何在ASP.NET WebControl的“内容”内部属性中包含其他标记?

  •  4
  • djdd87  · 技术社区  · 14 年前

    我已经搜索过这个网站,我找不到解决我问题的方法,所以如果问题已经被回答了,我很抱歉(我肯定以前有人问过这个问题)。

    我编写了一个jQuery弹出窗口,打包成WebControl和IScriptControl。最后一步是能够在控件的标记中写入标记。我已经使用过innerproperty属性几次,但只用于包含强类型类的列表。

    这是我在WebControl上的属性:

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public something??? Content
    {
       get
       {
          if (_content == null)
          {
             _content = new something???();
          }
          return _content;
       }
    }
    private something??? _content;
    

    以下是我所追求的HTML标记:

       <ctr:WebPopup runat="server" ID="win_Test" Hidden="false" Width="100px" Height="100px"
          Modal="true" WindowCaption="Test Window" CssClass="window">
          <Content>
             <div style="display:none;">
                <asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
             </div>
             <%--Etc--%>
             <%--Etc--%>
          </Content>
       </ctr:WebPopup>
    

    不幸的是,我不知道我的内容属性应该是什么类型。我基本上需要复制 UpdatePanel ContentTemplate .

    编辑 :因此,下面允许自动创建模板容器,但不显示任何控件,我所做的操作有什么问题?

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ITemplate Content
    {
        get
        {
            return _content;
        }
        set
        {
            _content = value;
        }
    }
    private ITemplate _content;
    

    编辑2 :重写createChildControls允许呈现项板中的控件:

    protected override void CreateChildControls()
    {
       if (this.Content != null)
       {
          this.Controls.Clear();
          this.Content.InstantiateIn(this);
       }
       base.CreateChildControls();
    }
    

    不幸的是,我现在无法从文件上的codebehind文件访问itemplate中的控件。也就是说,如果我把一个按钮放在标记内,那么:

    <ctr:WebPopup runat="server" ID="win_StatusFilter">
       <Content>
          <asp:Button runat="server" ID="btn_Test" Text="Cannot access this from code behind?" />
       </Content>
    </ctr:WebPopup>
    

    我就不能进入 btn_Test 从后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
       btn_Test.Text = "btn_Test is not present in Intellisense and 
          is not accessible to the page. It does, however, render correctly.";
    }
    

    编辑3 :已修复!编辑2是正确的解决方案。这只是一个视觉工作室2010年是一个痛苦的臀部。关闭应用程序并重新打开,页面上可以访问内容属性中的所有控件。

    编辑4 :编辑2没有解决问题。我已经试过了 [TemplateInstance(TemplateInstance.Single)] 属性之前有人提到过它,但当时我不认为它有什么不同。看起来2010年的视觉工作室今天很奇怪。

    因为我移除了标签并继续工作,所以我假设属性没有产生任何影响。由于再次返回代码,控件变得不可用。将属性添加回允许它全部工作,并且控件可以访问服务器端。 疯狂 . 我将接受布莱恩的回答,因为他在别人面前提到了解决方案。

    2 回复  |  直到 13 年前
        1
  •  5
  •   Nightfirecat peSHIr    13 年前

    公共项目板内容

    然后您将其呈现给用户界面,如下所示:

    Label label = new Label();
    this.Content.InstantiateIn(label);
    
    //Render label
    

    编辑:确保模板还定义

    [TemplateInstance(TemplateInstance.Single)]
    

    因为这允许您直接访问模板中的控件。

        2
  •  1
  •   VMAtm    14 年前

    您应该尝试使用:

    win_StatusFilter.FindControl("btn_Test") // this will be a Control
    win_StatusFilter.FindControl("btn_Test") as Button // this will be a Button if control found, otherwise it will be null.
    

    否则,您应该为控件定义一些属性,如本文中所述: http://msdn.microsoft.com/ru-ru/library/36574bf6%28v=VS.90%29.aspx


    更新: 根据本文中关于UpdatePanel的ContentTemplate属性的说明:

    http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.contenttemplate(v=VS.90).aspx

    您可以从ContentTemplate获取控件,因为TemplateInstanceAttribute值(UpdatePanel.ContentTemplate具有 TemplateInstance.Single )

    所以您应该只使用以下代码:

    [PersistenceMode(PersistenceMode.InnerProperty)] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    [TemplateInstance(TemplateInstance.Single)]
    public ITemplate Content
    

    更多信息,请访问:

    http://msdn.microsoft.com/ru-ru/library/system.web.ui.templateinstanceattribute(v=VS.90).aspx

    推荐文章