使用模板的UserControl,更改服务器控件的呈现ID [英] UserControl with templates, change server-control rendered ID

查看:54
本文介绍了使用模板的UserControl,更改服务器控件的呈现ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR ...

使用一次性模板创建 [UserControl] 时,是否可以将make服务器端控件放置在这些模板中以呈现其 id UserControl 还是容器ID?

When creating a [UserControl] with single use templates, is it possible to get make server-side controls placed within those templates to render their id without the UserControl or container IDs?

在我的ASP.NET Web应用程序中,我创建了一个具有多个一次性模板的 UserControl (我仅在其中提供了其中一个模板的代码)...

In my ASP.NET web application I've create a UserControl with multiple single-use template (I've only given the code here for one of those templates)...

Public Class MyUserCtrl
    Inherits System.Web.UI.UserControl

    Private Class MyUserCtrlLiteralContainer
        Inherits Control
        Implements INamingContainer
    End Class

    <TemplateContainer(GetType(MyUserCtrlLiteralContainer)),
     TemplateInstance(TemplateInstance.Single),
     PersistenceMode(PersistenceMode.InnerProperty)>
    Public Property FirstTemplate As ITemplate

    Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
        If FirstTemplate IsNot Nothing Then
            Dim container As New MyUserCtrlLiteralContainer()
            FirstTemplate.InstantiateIn(container)
            plhFirst.Controls.Add(container)
        End If
    End Sub
End Class    

(请注意, plhFirst 是标记中的< asp:PlaceHolder> )

(Note, plhFirst is a <asp:PlaceHolder> in the mark-up)

这是在页面中使用控件的示例(由 Master 页面控制)...

This is an example of the control being used in a page (controlled by a Master page)...

<asp:Content runat="server" ID="content" ContentPlaceHolderID="mainContent">
  <uc1:MyUserCtrl runat="server" id="muc1">
    <FirstTemplate>
      <asp:Button runat="server" id="btnMyButton" />
    </FirstTemplate>
  </uc1:MyUserCtrl>
</asp:Content>

< asp:Button> 呈现到HTML时,该元素的 id 会导致...

When the <asp:Button> is rendered to the HTML, the id of the element results in...

id="ctl00_content_muc1_ctl00_btnMyButton"

...,其中第二个 ctl00 MyUserCtrlLiteralContainer 实例的ID.

... where the second ctl00 is the ID of the MyUserCtrlLiteralContainer instance.

有什么办法使控件的 id 呈现如下?(因此不使用容器的ID)

Is there any way to make the id of the control render as follows? (So the ID of the container is not used)

ctl00_content_muc1_btnMyButton

或更妙的是,有什么办法使控件的 id 呈现如下?(因此,既不使用用户控件的ID,也不使用容器的ID)

Or even better, is there any way to make the id of the control render as follows? (So neither the ID of the usercontrol or container is used)

ctl00_content_btnMyButton

或者,是否存在一种不同的方法来实例化 UserControl 中的控件,从而导致上述任何一种情况?

Alternatively, is there a different way to instantiate the controls in the UserControl that would result in either of the above?

推荐答案

按钮 id 包括 muc1 ctl00 ,因为UserControl和MyUserCtrlLiteralContainer实现了 INamingContainer .如果您不希望 id 包括那些部分,但您 do 希望 id 包括其他祖先ID,那么您可以'不要使用这些控件类型.

The button id includes muc1 and ctl00 because UserControl and MyUserCtrlLiteralContainer implement INamingContainer. If you don’t want the id to include those parts, but you do want the id to include other ancestor IDs, then you can’t use those control types.

删除 ctl00 ::除非您特别需要自定义容器控件,否则可以删除MyUserCtrlLiteralContainer并直接在PlaceHolder中实例化该模板.

Removing ctl00: Unless you specifically need a custom container control, you can delete MyUserCtrlLiteralContainer and just instantiate the template directly in the PlaceHolder.

删除 muc1 ::您必须将UserControl重写为未实现INamingContainer的服务器控件.

Removing muc1: You must rewrite your UserControl as a server control that doesn't implement INamingContainer.

这是一个示例实现:

<ParseChildren(True)>
Public Class MyUserCtrl
    Inherits Control

    Private plhFirst As PlaceHolder

    <TemplateInstance(TemplateInstance.Single),
     PersistenceMode(PersistenceMode.InnerProperty)>
    Public Property FirstTemplate As ITemplate

    Private Sub Page_Init(sender As Object, e As EventArgs) Handles MyBase.Init
        plhFirst = New PlaceHolder()
        Controls.Add(plhFirst)

        If FirstTemplate IsNot Nothing Then
            FirstTemplate.InstantiateIn(plhFirst)
        End If
    End Sub

    Protected Overrides Sub Render(writer As HtmlTextWriter)
        ' <div class="myUserCtrl">
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "myUserCtrl")
        writer.RenderBeginTag(HtmlTextWriterTag.Div)

        ' <div class="firstControls">
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "firstControls")
        writer.RenderBeginTag(HtmlTextWriterTag.Div)

        plhFirst.RenderControl(writer)

        ' </div>
        writer.RenderEndTag()

        ' </div>
        writer.RenderEndTag()
    End Sub
End Class

注意:为避免可能的ID冲突,我特意保留了 plhFirst.ID .

Note: To avoid possible ID conflicts, I intentionally leave plhFirst.ID unset.

这篇关于使用模板的UserControl,更改服务器控件的呈现ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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