开发者

control not found in custom template

I take a user control file. I put this

<div  style="width:100%;">
    <div style="width:100%;">
        <asp:PlaceHolder ID="Header" runat="server" />
    </div>
    <div style="width:65%;float:left;">
        <asp:PlaceHolder ID="LeftSide" runat="server" />
    </div>
    <div style="width:35%;float:right;padding-top:20px;">
        <asp:PlaceHolder ID="RightSide" runat="server" />
    </div>
    <div style="width:100%;">
        <asp:PlaceHolder ID="Footer" runat="server" />
    </div>
</div>

In code-behind

Imports System.ComponentModel

Partial Public Class MainTemplate
    Inherits System.Web.UI.UserControl

    Private HeaderTemplateVar As ITemplate = Nothing
    Private LeftTemplateVar As ITemplate = Nothing
    Private RightTemplateVar As ITemplate = Nothing
    Private FooterTemplateVar As ITemplate = Nothing
    Private HeaderWidthVar As Integer

    <TemplateContainer(GetType(Container)), PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property HeaderTemplate() As ITemplate

        Get
            Return HeaderTemplateVar
        End Get
        Set(ByVal value As ITemplate)
            HeaderTemplateVar = value
        End Set

    End Property

    <TemplateContainer(GetType(Container)), PersistenceMode(PersistenceMode.InnerProperty)> _
   Public Property FooterTemplate() As ITemplate

        Get
            Return FooterTemplateVar
        End Get
        Set(ByVal value As ITemplate)
            FooterTemplateVar = value
        End Set

    End Property

    <TemplateContainer(GetType(Container)), PersistenceMode(PersistenceMode.InnerProperty)> _
   Public Property RightTemplate() As ITemplate

        Get
            Return RightTemplateVar
        End Get
        Set(ByVal value As ITemplate)
            RightTemplateVar = value
        End Set

    End Property

    <TemplateContainer(GetType(Container)), PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property LeftTemplate() As ITemplate

        Get
            Return LeftTemplateVar
        End Get
        Set(ByVal value As ITemplate)
            LeftTemplateVar = value
        End Set

开发者_JAVA百科    End Property

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Not HeaderTemplate Is Nothing Then

            Dim HeaderContainer As New Container()
            HeaderTemplate.InstantiateIn(HeaderContainer)
            Header.Controls.Add(HeaderContainer)

        End If

        If Not LeftTemplate Is Nothing Then

            Dim LeftContainer As New Container()
            LeftTemplate.InstantiateIn(LeftContainer)
            LeftSide.Controls.Add(LeftContainer)

        End If

        If Not RightTemplate Is Nothing Then

            Dim RightContainer As New Container()
            RightTemplate.InstantiateIn(RightContainer)
            RightSide.Controls.Add(RightContainer)

        End If

        If Not FooterTemplate Is Nothing Then

            Dim FooterContainer As New Container()
            FooterTemplate.InstantiateIn(FooterContainer)
            Footer.Controls.Add(FooterContainer)

        End If

    End Sub


End Class

When I call this in UI, I put a button on the UI page that control is not appear in code-behind of UI. I get this button event in code-behind.

<Yoma:Template ID="Test1" runat="server">
        <HeaderTemplate>
            <div  style="background-color:Silver;color:Red;width:100%;">Welcome</div>
        </HeaderTemplate>
        <LeftTemplate>
                <p>he 1962 South Vietnamese Independence Palace bombing in Saigon was an aerial attack on 
                February 27, 1962, by two dissident Vietnam Air Force pilots, Second Lieutenant Nguyễn Văn 
                political association. Domestically, the incident was 
                reported to have increased plotting against Diem by his officers. (more...)</p>
        </LeftTemplate>
        <RightTemplate>
            <table align="right" border="0" cellspacing="0" cellpadding="10" style="width:300px; border: 1px Solid #78bafa;" bgcolor="#f3f7fa" id="AddUser">
                <tr>
                    <td align="left">
                        <table style="width:260px;" border="0" cellspacing="0" cellpadding="3">
                            <tr>
                                <td height="30" colspan="2" valign="top">
                                    <table border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td ><strong>Add Respondent</strong></td>
                                            <td align="right"></td> 
                                        </tr>
                                    </table>
                                </td>
                            </tr> 
                            <tr>
                                <td width="75"  >First Name:</td>
                                <td width="185"  ><input name="FirstName" type="text" runat="server" id="FirstName" size="8" /></td>
                            </tr>
                            <tr>
                                <td  >Surname:</td>
                                <td  ><input name="Surname" type="text" id="Surname" size="12" /></td>
                            </tr>
                            <tr>
                                <td  >Email:</td>
                                <td  ><input name="Email" type="text" id="Email" size="17" /></td>
                            </tr>
                            <tr>
                                <td colspan="2" ><br />
                                    <asp:Button ID="AddRespondentButton" runat="server" Text="ADD RESPONDENT" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </RightTemplate>
        <FooterTemplate>
            <table style="width:900px;margin:0px 0px 0px 0px;" border="0" align="center" cellpadding="3" cellspacing="0">
                <tr>
                    <td >&copy; 2006-<%=Now.Date%> Easi-Answers Limited</td>
                </tr>
            </table>
        </FooterTemplate>
    </Yoma:Template>


In case of templated controls, template may get instantiated multiple times. Therefore, you will not find control references in generated code behind - because control gets created at runtime when template is instantiated. Such control references has to be found using FindControl method at template container (RightContainer in your case).

All you want is to attach event handler then you may use syntax such as

<asp:Button ID="AddRespondentButton" runat="server" Text="ADD RESPONDENT" OnClick="AddRespondentButton_Click" />

AddRespondentButton_Click will be an protected/public event handler method in the page code behind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜