TL;DR...
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?
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
(Note, plhFirst
is a <asp:PlaceHolder>
in the mark-up)
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>
When the <asp:Button>
is rendered to the HTML, the id
of the element results in...
id="ctl00_content_muc1_ctl00_btnMyButton"
... where the second ctl00
is the ID of the MyUserCtrlLiteralContainer
instance.
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
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
Alternatively, is there a different way to instantiate the controls in the UserControl
that would result in either of the above?