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

  • Thread starter Thread starter Alan Silver
  • Start date Start date
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.
 
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.
 
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.
 
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.
 
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.
 

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

Back
Top