自定义模板中找不到控件

时间:2010-12-15 07:35:29

标签: asp.net

我带了一个用户控制文件。我把这个

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

在代码隐藏

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

    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

当我在UI中调用它时,我在UI页面上放了一个按钮,该控件没有出现在UI的代码隐藏中。 我在代码隐藏中得到了这个按钮事件。

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

1 个答案:

答案 0 :(得分:2)

对于模板化控件,模板可能会多次实例化。因此,您将无法在生成的代码中找到控件引用 - 因为控件是在实例化模板时在运行时创建的。必须在模板容器(在您的情况下为RightContainer)中使用FindControl方法找到此类控件引用。

您想要的只是附加事件处理程序,然后您可以使用

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

AddRespondentButton_Click将成为后面页面代码中的受保护/公共事件处理程序方法。