ASP NET Custom Control with PersistenceMode.InnerProperty including Server Controls

Controls Server including PersistenceMode.InnerProperty with Control Custom NET ASP

If we want to use a Custom Control with PersistenceMode.InnerProperty. And would like to include some Asp Server Controls we do following:

We create a aspx where the controls to be added:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default9.aspx.cs" 
Inherits="Default9" %>

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" 
tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <uc1:WebUserControl ID="WebUserControl1" runat="server" >
        <FirstLevelInnerString>
            <asp:Literal ID="lita" Text="Halo" runat="server">
            </asp:Literal><br />
            <asp:LinkButton ID="vdsvs" Text="link" runat="server">
            </asp:LinkButton>
        </FirstLevelInnerString>
        <SecondLevelInnerString>Hallo vom</SecondLevelInnerString>
    </uc1:WebUserControl>
    <div>
    </div>
    </form>
</body>
</html>

 

After that create a usercontrol where yout put the following markup:

 

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:PlaceHolder ID="Title" runat="server"></asp:PlaceHolder>
<asp:Literal id="litSecondLevelInnerString" runat="server"></asp:Literal>

 

Code:

 

[ParseChildren(true)]
public partial class WebUserControl : System.Web.UI.UserControl
{
    private ITemplate _title;

    [PersistenceMode(PersistenceMode.InnerProperty), 
    TemplateContainer(typeof(TemplateControl))]
    public ITemplate FirstLevelInnerString
    {
        get { return _title; }
        set { _title = value; }
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string SecondLevelInnerString
    {
        set
        {
            this.litSecondLevelInnerString.Text = value + 
                " SecondLevelInnerString";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (_title != null)
        {
            _title.InstantiateIn(Title);
        }
    }
}

Thats it!

Now you can use Server Contols in PersistenceMode.InnerProperty Mode.



Comments are closed