473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to address control within templated custom control from code behind

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>


Nov 18 '05 #1
2 1439
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.
"Earl Teigrob" <ea******@hotmail.com> wrote in message news:ej**************@TK2MSFTNGP09.phx.gbl...
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>



Nov 18 '05 #2
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

"Raterus" <ra*****@spam.org> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
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.
"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
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>

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1461
by: Scott C. Reynolds | last post by:
Hi. I have created a templated control, and I want to know how i can set it up so that the projects consuming it will have all the same intellisense benefits of using any other server control. ...
0
357
by: John Crowley | last post by:
I keep running into this over and over again... I want a block server control that renders a header and footer, and child controls in between. But I don't want a templated control, for the...
0
1524
by: Earl Teigrob | last post by:
I have created a templated custom control that simply creates a spacing table around other objects. This control works fine for most uses but when I add it inside the ItemTemplate of a DataGrid...
1
1427
by: Chris Newby | last post by:
I have a very basic custom control that exposes an ITemplate property and everything seems pretty straight forward so far. But, when I put other server controls intro the template section of my...
3
1838
by: Andy Fish | last post by:
Hi, Say I had a repeater-like situation but were I wanted to have every 3rd item look different instead of every alternating one. I could write my own custom control and in the ASPX file...
2
2022
by: Andy Fish | last post by:
Hi, First some background: When you databind a repeater control, the controls within the template are given an id like Repeater1:_ctl<n>:Button1 where <n> increments for each repeater item. If...
5
3565
by: serge calderara | last post by:
Dear all, I am new in asp.net and prepare myself for exam I still have dificulties to understand the difference between server control and HTML control. Okey things whcih are clear are the fact...
15
6472
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
11
3254
by: Nick Gilbert | last post by:
Hi, How can I create a custom control which will wrap its content in a header and footer? eg: Is it possible to create a .NET user control which can surround other controls? eg:...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.