How to address control within templated custom control from code behind

E

Earl Teigrob

I have created a simple custom control that uses a table to create space
around its contents. The code in the aspx page is show below. My problem is
that the controls within the template can not be directly accessed from the
code behind. In the code listing below, trying to use "Literal1" control in
the code behind results in a null reference exception. How do I make the
controls within my templated custom control accessible in the code behind?

Thanks

Earl

<cc1:SpacerControl id="SpacerControl1" SpaceAbove="3" SpaceBelow="3"
SpaceRight="100" SpaceLeft="100" WidthInPercent="false" FrameColor="blue"
TableAlign="Center" ContentsWidth="500px" TotalWidth="75%" runat="server">
<ITEMTEMPLATE>
Some Text
<asp:Literal id="Literal1" runat="server"></asp:Literal>
</ITEMTEMPLATE>
</cc1:SpacerControl>
 
R

Raterus

You'll have to cast the specific reference to the control in your controls "controls" collection

dim lit as literal
lit = DirectCast(mySpacerControl.Controls(1), Literal) <-- Something like that anyway, getting the appropriate index is the tricky part.
 
E

Earl Teigrob

Raterus,

Thank you, I got it. Though I had trouble finding the correct index value, I
used the handy "FindControl" method and it worked perfectly! Code looks like
this (in C#)
Literal Literal1 = (Literal)SpacerControl1.FindControl("Literal1");

One more obstacle overcome!

Earl



You'll have to cast the specific reference to the control in your controls
"controls" collection

dim lit as literal
lit = DirectCast(mySpacerControl.Controls(1), Literal) <-- Something like
that anyway, getting the appropriate index is the tricky part.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top