ASP.NET:可以创建一个用户控件,包括标记里面的内容? [英] ASP.NET : Possible to create a UserControl that include inside markup content?

查看:131
本文介绍了ASP.NET:可以创建一个用户控件,包括标记里面的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的东西谷歌是无法给予答案。我试图创建一个asp.net用户控件,当我把内容放到它的开放密切标签将它列入我到现在还能够接取它从父ID的内容。下面是我想要达到为例。

I'm trying to achieve something google isn't able to give answer. I'm trying to create an asp.net usercontrol that when I put content into it's open close tag will include it for me to be still able to acess it's content by ID from the Parent. Here is an exemple of what I want to achieve.

用户控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ctrlGENE_CollapsiblePanel.ascx.cs"
    Inherits="Controls_GenericsControls_ctrlGENE_CollapsiblePanel" %>
<asp:Panel ID="pnlHeader" CssClass="SnapPanelHeader" runat="server">
    <h3>
        <asp:Literal ID="litTitle" Text='<%# Text %>' runat="server" />
    </h3>
    <div class="SnapPanelHeader-img">
        <img id="imgOpenClose" src="/images/application/close.jpg" alt="" />
    </div>
</asp:Panel>
<asp:PlaceHolder runat="server" ID="pnlContent">
// **HERE I WANT TO INCLUDE THE INSIDE MARKUP**
</asp:PlaceHolder>
<act:CollapsiblePanelExtender runat="server" TargetControlID="pnlContent" ImageControlID="imgOpenClose"
    ExpandControlID="imgOpenClose" CollapseControlID="imgOpenClose" CollapsedImage="/images/application/open.jpg"
    ExpandedImage="/images/application/close.jpg" CollapsedSize="0" TextLabelID="litTitle">
</act:CollapsiblePanelExtender>

父页,其中包括控制:

Parent Page that include control :

<uc:MyUserControl ID="clpTest" runat="server">
     <asp:Literal ID="litText" runat="server" />
</uc:MyUserControl>

喜欢我将能够在Parent.cs文件来完成:

Like this I would be able in the Parent.cs file to do :

litText.Text = "Anything";

我知道我们可以实现与这里的ITemplate接口作为显示类似的东西: HTTP ://msdn.microsoft.com/en-us/library/36574bf6.aspx

这应该是这样的:

<uc:MyUserControl ID="clpTest" runat="server">
     <ItemTemplate>
          <asp:Literal ID="litText" runat="server" />
     </ItemTemplate>
</uc:MyUserControl>

但如果我这样做,我不会能够访问litText的财产,我能达到的唯一途径是用的FindControl,我希望避免的。

But If I do this I wouldn't be able to access the property of litText and the only way I could reach it is with a FindControl, which I want to avoid.

任何人有这样的提示,如果我能达到我的目标这样或那样的?

Anyone have a hint on this if I can reach my goal one way or another?

感谢您

推荐答案

好吧我固定在这里是我使用的解决方案:

Ok I fixed it here is the solution I used :

在MyUserConstrol.ascx文件我已经把一个占位符,我想innerHTML来显示:

In MyUserConstrol.ascx file I have put a placeholder where I wanted innerHTML to show :

<asp:PlaceHolder runat="server" ID="plhContent" />

然后在MyUserControl.ascx.cs文件我添加的属性类:

Then in the MyUserControl.ascx.cs file I added those attribute to the class:

[ParseChildren(false)]
[PersistChildren(true)]

和我添加到该文件中:

public void RegisterUpdatePanel(UpdatePanel panel)
{
    MethodInfo m =
        (from methods in typeof (ScriptManager).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
         where methods.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")
         select methods).First();

    m.Invoke(ScriptManager.GetCurrent(Page), new object[] {panel});
}

protected override void CreateChildControls()
{
    for (int i = 0; i < Controls.Count; i++)
        if (Controls[i] is MyLastControl)
            while (i + 2 < Controls.Count)
            {
                // In cas there is an updatepanel in the control we are moving
                // We are registering an event to register the updatepanel
                // to the scriptmanager again
                SearchUpdatePanel(Controls[i + 2]);
                plhContent.Controls.Add(Controls[i + 2]);
            }
    base.CreateChildControls();
}

private void SearchUpdatePanel(Control control)
{
    if (control is UpdatePanel)
        control.Unload += updPnl_Unload;

    foreach (Control childcontrol in control.Controls)
        SearchUpdatePanel(childcontrol);
}

protected void updPnl_Unload(object sender, EventArgs e)
{
    RegisterUpdatePanel((UpdatePanel) sender);
}

请注意,我叫我的ASCX文件myLastContol最后控制,知道什么是的innerHTML注射前的控制,因为我不知道我收到什么。在用户控件的控件的程序基本上是这样的循环,当你将到达用户控件的结束,一切之后的innerHTML从父所以采取那些并将其移动到那里我希望他们的占位符。

Note that I named my last control in the ascx file myLastContol to know what was the control before the innerHTML injection since I don't know what I'm receiving. The the procedure basically say loop in the controls in the UserControl, when you will reach the end of UserControl, everything after is innerHTML from parent so take those and move them to the placeholder where I want them.

我希望这会帮助别人。

下面是一些消息来源我来实现我的目标:
<一href=\"http://msmvps.com/blogs/luisabreu/archive/2006/11/16/adding-removing-updatepanels-dynamicaly-from-a-page.aspx\"相对=nofollow>注册的UpdatePanel

Here is some sources I used to achieve my goal : Registering UpdatePanel

这篇关于ASP.NET:可以创建一个用户控件,包括标记里面的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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