我怎样才能有一个ASP.Net的WebControl的“内容”内在属性中额外的标记? [英] How can I include additional markup within a 'Content' inner property of an ASP.Net WebControl?

查看:148
本文介绍了我怎样才能有一个ASP.Net的WebControl的“内容”内在属性中额外的标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索的网站,我无法找到我的问题的解决方案,因此,如果它已经被回答的歉意(我敢肯定,一定是有人问这之前)。

I've searched the site and I cannot find a solution for my problem, so apologies if it's already been answered (I'm sure someone must have asked this before).

我写,我已经打包成一个的WebControl和IScriptControl一个jQuery弹出窗口。最后一步是要能够控制我的标签内写入的标记。我用InnerProperty属性几次,但仅限于包括强类型的类的列表。

I have written a jQuery Popup window that I've packaged up as a WebControl and IScriptControl. The last step is to be able to write the markup within the tags of my control. I've used the InnerProperty attribute a few times, but only for including lists of strongly typed classes.

下面是我对物业的WebControl:

Here's my property on the WebControl:

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

下面是什么,我以后是HTML标记:

Here's the HTML Markup of what I'm after:

   <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

修改:那么下面允许自动创建一个模板容器,但没有控件显示出来,有什么错我在做什么。

EDIT: So the following allows a Template container to be automatically created, but no controls show up, what's wrong with what I'm doing?

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

EDIT2 :覆盖的CreateChildControls允许控制了Itemplate内呈现:

EDIT2: Overriding the CreateChildControls allows the controls within the ITemplate to be rendered:

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

可惜我现在不能从文件上的codebehind文件访问的ITemplate中的控件。即如果我把我的标志内的一个按钮,像这样:

Unfortunately I cannot now access the controls within the ITemplate from the codebehind file on the file. I.e. if I put a button within my mark as so:

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

然后我不能从后面code访问 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.";
}

EDIT3 :修复!编辑2是正确soluion。这只是视觉工作室2010是在臀部疼痛。关闭应用程序,并重新打开它和我的内容属性中的所有控件都在页面上访问。

EDIT3: FIXED! Edit 2 is the correct soluion. It was just Visual Studios 2010 being a pain in the buttocks. Closed the app and reopened it and all my controls within the Content property were accessible on the page.

EDIT4 :编辑2并没有解决问题。我已经尝试了 [模板实例(TemplateInstance.Single)] 属性任何人提到过它之前,但当时我没想到它已经发挥了作用。看来视觉工作室2010年只是被怪异的今天。

EDIT4: Edit 2 didn't fix the issue. I had already tried the [TemplateInstance(TemplateInstance.Single)] attribute before anyone had mentioned it, however at the time I didn't think it had made a difference. It appears Visual Studios 2010 is just being weird today.

由于我删除了标记,并进行了工作,我以为属性没有作出区别。由于要回code再次控制已变得不可用。添加属性回来让这一切工作,为控件可访问服务器端。 MADNESS 。我将接受布赖恩的答案,因为他在其他人之前提到的修补程序。

Since I removed the tag and it carried on working, I assumed the attribute hadn't made a difference. Since going back to the code AGAIN the controls have become unavailable. Adding the attribute back in allowed it all to work and for the controls to be accessible server side. MADNESS. I will be accepting Brian's answer as he mentioned the fix before anyone else.

推荐答案

公开了Itemplate内容

public ITemplate Content

那么你呈现到用户界面,类似:

which then you render to the UI like:

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

//Render label

编辑:确保模板还定义了

Make sure the template also defines

[TemplateInstance(TemplateInstance.Single)]

,因为这可以让你在模板中访问直接控制。

as this allows you to access the controls within the template directly.

这篇关于我怎样才能有一个ASP.Net的WebControl的“内容”内在属性中额外的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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