Textbox control inside repeater


I am sure this is a n00b question, but it feels like years that I have been trying to sort this out.

Basically, I have got a TextBox inside a Repeater Control. In the codebehind of a button I want to change the textbox:

txtContactName.ReadOnly = true;

Intellisense cant find the txtContactName and if I just type it in, then I get this error.

The name 'txtContactName' does not exist in the current context	

If anyone can enlighten me, then I will be so glad!


You have to change the textbox’s properties at the right moment. You can google for ItemCreated and ItemDataBound to give you some ideas.

Sorry I can’t help you more, but it’s been a couple of years since I did this kid of stuff.

The textbox is created dynamically at run time by the repeater, that’s why you can’t access it. It can be done what you want to do but it’s rather ugly if you want to access just one textbox from the list. Maybe you need to rethink that bit.

Actually, it is not hard to do this. Basically, you need to use the button to “find” the textbox, presuming the button is in the same RepeaterItem as the text box:

protected void ButtonClickingHandler(object sender, EventArgs e)
     Control s=(Control)sender;
     TextBox tb=(TextBox)s.NamingContainer.FindControl("txtContactName");

If you need to perform this on the whole set of items, you can iterate through the repeater’s Items collection and do pretty much the same thing.

wwb_99’s code works if I place the button inside the repeater, but then other code breaks when the button is press because that button is used as a trigger elsewhere.

Is there anything else that I can use instead of the repeater to display data in textboxs where they arent generated at run time. Or do they all (repeater, detailsview, formview etc) generate the controls at run time and therefor have to do it all in the longwinded way as ‘thats just the way it is’ ?

Basically I wasnt to be able to press a button called ‘Update’, then it will make all the textboxes (about 10 of them) readonly = false.


I have figured out that its just going to be quicker to have 2 seperate pages for showing and updating. Not enough time to try and figure out silly stuff like this.

Cheers anyway!

That would have been the presumption I made. I covered the 2nd scenario in my previous post as well–it would go something like:

protected void MakeUpdatable(object sender, EventArgs e)
      foreach (RepeaterItem ri in Thangs.Items)
           TextBox tb=(TextBox)ri.FindControl("TextBox");

Possibly an easier way would be to just make the button flip a page level variable, bind the appropriate enabled properties to that variable, and then re-bind the repeater.

Very Great Thanks wwb_99!!!
It is the simplest way to find control inside Repeater!