Textbox Control in a Repeater in ASP.Net

G

Guest

I am attempting to print out a table using a repeater in which one field is
updatable. Basically you would have a table of x number of rows and y number
of columns. However: only one field in each row will be updatable. My
thought was to build a repeater and then one of the <ItemTemplate> would be a
textbox where the user can update the values in each.

How would I go about in the code-behind to reference each textbox? My
texbox definintion is: <asp:Textbox ID="cID"......

Is an array created and I loop through the array? Just not sure how to
handle this.

Thanks for any help
Andy
 
O

Ollie Riches

why not loop thorugh the repeater item and find the control you want using
the control id and then set the readonly value as required.

i.e

for(int i = 0; i < repeater.Items.Count; i++)
{
TextBox tb = repeater.Items.FindControl("cID");
...
...
}

HTH

Ollie Riches
 
G

Guest

Thanks for the response.

Originally I thought I was doing something wrong: as I had the following
line:
foreach(RepeaterItem dataItem in rpRepeater.Items)

which was not bringing anything back. However: when I use your line as
well: the count is returned at 0 and the for loop exits immediately.

When viewing the page, I have upwards of 45 rows, so I know there are items.
What am I missing?

Thanks
Andy

Ollie Riches said:
why not loop thorugh the repeater item and find the control you want using
the control id and then set the readonly value as required.

i.e

for(int i = 0; i < repeater.Items.Count; i++)
{
TextBox tb = repeater.Items.FindControl("cID");
...
...
}

HTH

Ollie Riches

Andy said:
I am attempting to print out a table using a repeater in which one field is
updatable. Basically you would have a table of x number of rows and y
number
of columns. However: only one field in each row will be updatable. My
thought was to build a repeater and then one of the <ItemTemplate> would
be a
textbox where the user can update the values in each.

How would I go about in the code-behind to reference each textbox? My
texbox definintion is: <asp:Textbox ID="cID"......

Is an array created and I loop through the array? Just not sure how to
handle this.

Thanks for any help
Andy
 
G

Guest

NEvermind: your code was working. I had the ViewState=false which was
causing the issue in the backend.

Sorry for all the dumb questions ;-)

Thanks so much for the help though. I appreciaate it, but it was another
ID-10T error ;-)

Ollie Riches said:
why not loop thorugh the repeater item and find the control you want using
the control id and then set the readonly value as required.

i.e

for(int i = 0; i < repeater.Items.Count; i++)
{
TextBox tb = repeater.Items.FindControl("cID");
...
...
}

HTH

Ollie Riches

Andy said:
I am attempting to print out a table using a repeater in which one field is
updatable. Basically you would have a table of x number of rows and y
number
of columns. However: only one field in each row will be updatable. My
thought was to build a repeater and then one of the <ItemTemplate> would
be a
textbox where the user can update the values in each.

How would I go about in the code-behind to reference each textbox? My
texbox definintion is: <asp:Textbox ID="cID"......

Is an array created and I loop through the array? Just not sure how to
handle this.

Thanks for any help
Andy
 
S

Scott Roberts

Andy said:
I am attempting to print out a table using a repeater in which one field is
updatable. Basically you would have a table of x number of rows and y number
of columns. However: only one field in each row will be updatable. My
thought was to build a repeater and then one of the <ItemTemplate> would be a
textbox where the user can update the values in each.

How would I go about in the code-behind to reference each textbox? My
texbox definintion is: <asp:Textbox ID="cID"......

Is an array created and I loop through the array? Just not sure how to
handle this.


For setting the value, use the ItemDataBound event:

private void Repeater1_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if ( e.Item.ItemType == ListItemType.Item )
{
TextBox myTextBox = (TextBox)e.Item.FindControl("TextBoxName");
myTextBox.Text = "Some Text"; // use "e.Item.DataItem" to get data from
a bound data source.
}
}


To read the user's changes:

foreach ( RepeaterItem item in Repeater1.Items )
{
if ( item.ItemType == ListItemType.Item)
{
TextBox myTextBox = (TextBox)e.Item.FindControl("TextBoxName");
// do something with "myTextBox.Text"
}
}
 

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

Top