How do I get at a control in the header template of a repeater?

A

Alan Silver

Hello,

If I have a control in the header template of a repeater, how do I get
at this in code? I know how to do this for an ItemTemplate, you just
do...

txtFred = (TextBox)rptNpVars.Items.FindControl("txtFred");

where i is the index of the item. How do I do a similar thing for the
header? Following a post I found in the archives, I tried ...

txtFred = (TextBox)rptNpVars.Controls[0].FindControl("txtFred");

but this gave an error "Specified argument was out of the range of valid
values. Parameter name: index".

Thanks for any help.
 
C

Curt_C [MVP]

Alan said:
Hello,

If I have a control in the header template of a repeater, how do I get
at this in code? I know how to do this for an ItemTemplate, you just do...

txtFred = (TextBox)rptNpVars.Items.FindControl("txtFred");

where i is the index of the item. How do I do a similar thing for the
header? Following a post I found in the archives, I tried ...

txtFred = (TextBox)rptNpVars.Controls[0].FindControl("txtFred");

but this gave an error "Specified argument was out of the range of valid
values. Parameter name: index".

Thanks for any help.


Have you tried the QuickWatch window on rptNpVars.Controls and seeing
what's in it? I dont know the index for the header template but I would
think it's 0 like you indicated, but see whats in rptNpVars.Controls[0]
by stepping into it in the watch window.
 
G

Guest

I'm not sure, but I think this index is -1. Just try :)

Another way is to use:

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Header) {

// txtFred = ((TextBox)e.Item.FindControl("txtFred")).Text;
}
}


--
C# Dev


Curt_C said:
Alan said:
Hello,

If I have a control in the header template of a repeater, how do I get
at this in code? I know how to do this for an ItemTemplate, you just do...

txtFred = (TextBox)rptNpVars.Items.FindControl("txtFred");

where i is the index of the item. How do I do a similar thing for the
header? Following a post I found in the archives, I tried ...

txtFred = (TextBox)rptNpVars.Controls[0].FindControl("txtFred");

but this gave an error "Specified argument was out of the range of valid
values. Parameter name: index".

Thanks for any help.


Have you tried the QuickWatch window on rptNpVars.Controls and seeing
what's in it? I dont know the index for the header template but I would
think it's 0 like you indicated, but see whats in rptNpVars.Controls[0]
by stepping into it in the watch window.
 
A

Alan Silver

If I have a control in the header template of a repeater, how do I
get at this in code? I know how to do this for an ItemTemplate, you
just do...
txtFred = (TextBox)rptNpVars.Items.FindControl("txtFred");
where i is the index of the item. How do I do a similar thing for
the header? Following a post I found in the archives, I tried ...
txtFred = (TextBox)rptNpVars.Controls[0].FindControl("txtFred");
but this gave an error "Specified argument was out of the range of
valid values. Parameter name: index".
Thanks for any help.


Have you tried the QuickWatch window on rptNpVars.Controls and seeing
what's in it? I dont know the index for the header template but I would
think it's 0 like you indicated, but see whats in rptNpVars.Controls[0]
by stepping into it in the watch window.


I'm not using VS, so I can't do this. I'm fairly new at ASP.NET and
didn't fancy spending all that money on VS 2003, when it's about to be
superseded by an apparently superior product.

Also, I find I learn what's going on a lot better by doing it all by
hand. I get to see what is needed first hand, rather than having an IDE
make changes without me knowing. I think I've learnt a lot more this way
than I would have with VS.

Thanks anyway.
 
A

Alan Silver

I'm not sure, but I think this index is -1. Just try :)

OK, I'll give it a go (on my way to bed, so I'm not trying it now!!).
Another way is to use:

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Header) {

// txtFred = ((TextBox)e.Item.FindControl("txtFred")).Text;
}
}

Which is fine inside the event, but what if you want to get at it
outside of that? In truth, I suspect that you wouldn't want it inside
the repeater then, but it's too late for my little brain to think this
through!!

Thanks for the reply.
 
Joined
Aug 12, 2009
Messages
1
Reaction score
0
Get Control from Header template of a Repeater

>> Let say you have below repeater control in your application.

<asp:Repeater ID="repeaterControl" runat="server">
<HeaderTemplate>
<asp:Label ID="LblDate" runat="server" Text=''></asp:Label>
</HeaderTemplate>
<ItemTemplate>
......
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>

>> Below is the code to get the control from the header template

foreach (Control c in repeaterControl.Controls)
{
RepeaterItem item = (RepeaterItem)c;
if (item.ItemType == ListItemType.Header)
{
Label lblCelebrationDate = (Label)item.FindControl("LblDate");
if (lblCelebrationDate != null)
{
// your code
}
}
break;
}

>> Hope this will help ....


-Priyen
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top