控制未在自定义模板发现 [英] control not found in custom template

查看:67
本文介绍了控制未在自定义模板发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < D​​IV的风格=宽度:100%;>
    < D​​IV的风格=宽度:100%;>
        < ASP:占位符ID =标题=服务器/>
    < / DIV>
    < D​​IV的风格=宽度:65%;浮动:左;>
        < ASP:占位符ID =LeftSide=服务器/>
    < / DIV>
    < D​​IV的风格=宽度:35%;浮动:权利;填充顶:20像素;>
        < ASP:占位符ID =RightSide=服务器/>
    < / DIV>
    < D​​IV的风格=宽度:100%;>
        < ASP:占位符ID =页脚=服务器/>
    < / DIV>
< / DIV>

在code-背后

 进口System.ComponentModel部分公共类MainTemplate
    继承System.Web.UI.UserControl    私人HeaderTemplateVar作为了Itemplate =无
    私人LeftTemplateVar作为了Itemplate =无
    私人RightTemplateVar作为了Itemplate =无
    私人FooterTemplateVar作为了Itemplate =无
    私人HeaderWidthVar作为整数    < TemplateContainer(的GetType(集装箱)),PersistenceMode(PersistenceMode.InnerProperty)GT; _
    公共属性HeaderTemplate中()作为了Itemplate        得到
            返回HeaderTemplateVar
        到底得的
        设置(BYVAL值作为了Itemplate)
            HeaderTemplateVar =价值
        结束设定    高端物业    < TemplateContainer(的GetType(集装箱)),PersistenceMode(PersistenceMode.InnerProperty)GT; _
   公共属性FooterTemplate()作为了Itemplate        得到
            返回FooterTemplateVar
        到底得的
        设置(BYVAL值作为了Itemplate)
            FooterTemplateVar =价值
        结束设定    高端物业    < TemplateContainer(的GetType(集装箱)),PersistenceMode(PersistenceMode.InnerProperty)GT; _
   公共财产RightTemplate()作为了Itemplate        得到
            返回RightTemplateVar
        到底得的
        设置(BYVAL值作为了Itemplate)
            RightTemplateVar =价值
        结束设定    高端物业    < TemplateContainer(的GetType(集装箱)),PersistenceMode(PersistenceMode.InnerProperty)GT; _
    公共财产LeftTemplate()作为了Itemplate        得到
            返回LeftTemplateVar
        到底得的
        设置(BYVAL值作为了Itemplate)
            LeftTemplateVar =价值
        结束设定    高端物业    私人小组Page_Init(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Init        如果没有HeaderTemplate中是没有那么            昏暗HeaderContainer作为新集装箱()
            HeaderTemplate.InstantiateIn(HeaderContainer)
            Header.Controls.Add(HeaderContainer)        万一        如果没有LeftTemplate是没有那么            昏暗LeftContainer作为新集装箱()
            LeftTemplate.InstantiateIn(LeftContainer)
            LeftSide.Controls.Add(LeftContainer)        万一        如果没有RightTemplate是没有那么            昏暗RightContainer作为新集装箱()
            RightTemplate.InstantiateIn(RightContainer)
            RightSide.Controls.Add(RightContainer)        万一        如果没有FooterTemplate是没有那么            昏暗FooterContainer作为新集装箱()
            FooterTemplate.InstantiateIn(FooterContainer)
            Footer.Controls.Add(FooterContainer)        万一    结束小组
末级

当我把这个UI中,我把该控制并非出现在用户界面的code-背后的UI页面上的一个按钮。
我得到这个按钮事件在code-落后。

 <佑玛:模板ID =测试1=服务器>
        <&HeaderTemplate中GT;
            < D​​IV的风格=背景颜色:银白色;颜色:红色;宽度:100%;>欢迎及LT; / DIV>
        < / HeaderTemplate中>
        < LeftTemplate>
                < P> 1962年,他在西贡南越独立宫轰炸空中袭击
                1962年2月27日,两名持不同政见越南空军飞行员,少尉阮文
                政治结社。从国内看,事发当时
                据报道,增加了他的军官密谋反对行乐。 (更多...)< / P>
        < / LeftTemplate>
        < RightTemplate>
            <表ALIGN =右边界=0CELLSPACING =0的cellpadding =10的风格=宽度:300像素;边界:1px的固体#78bafa; BGCOLOR =#f3f7faID =ADDUSER>
                &所述; TR>
                    < TD align =left>
                        <表样式=WIDTH:260px;边界=0CELLSPACING =0的cellpadding =3>
                            &所述; TR>
                                &所述; TD高度=30列跨度=2VALIGN =顶>
                                    <表格边框=0的cellpadding =0CELLSPACING =0>
                                        &所述; TR>
                                            < TD><强>添加投诉< / STRONG>< / TD>
                                            < TD ALIGN =右>< / TD>
                                        < / TR>
                                    < /表>
                                < / TD>
                            < / TR>
                            &所述; TR>
                                < TD WIDTH =75>首先名称:< / TD>
                                < TD WIDTH =185><输入名称=名字类型=文本=服务器ID =名字大小=8/>< / TD>
                            < / TR>
                            &所述; TR>
                                < TD>姓氏:其中; / TD>
                                < TD><输入名称=姓氏类型=文本ID =姓大小=12/>< / TD>
                            < / TR>
                            &所述; TR>
                                < TD>电子邮件:< / TD>
                                < TD><输入名字=电子邮件类型=文本ID =电子邮件大小=17/>< / TD>
                            < / TR>
                            &所述; TR>
                                < TD合并单元格=2>< BR />
                                    < ASP:按钮的ID =AddRespondentButton=服务器文本=添加答辩人/>
                                < / TD>
                            < / TR>
                        < /表>
                    < / TD>
                < / TR>
            < /表>
        < / RightTemplate>
        < FooterTemplate>
            <表样式=WIDTH:900px;保证金:0像素0像素0像素0像素;边界=0ALIGN =中心的cellpadding =3CELLSPACING =0>
                &所述; TR>
                    < TD>&安培;复印件; 2006-<%= Now.Date%GT;爱莎答案有限公司及LT; / TD>
                < / TR>
            < /表>
        < / FooterTemplate>
    < /佑玛:模​​板>


解决方案

在模板的控制时,模板可能会多次实例化。因此,你不会找到产生code控制引用背后 - 因为当模板实例被控制在运行时创建的。这样的控制引用必须使用的FindControl 方法在模板容器(在你的情况RightContainer)被发现。

所有你想要的是附加的事件处理程序,那么你可以使用语法如

 < ASP:按钮的ID =AddRespondentButton=服务器文本=添加答辩人的OnClick =AddRespondentButton_Click/>

AddRespondentButton_Click 将在页面code后面的保护/公共事件处理方法。

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

    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.

这篇关于控制未在自定义模板发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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