ASP.NET Accessing TextBox value within a nested repeater

Hi All!

ok, I have one repeater nested within another. I can successfully bind both the parent and the child to data and display this on the web page. I have a textbox associated with all the child elements but I am unsure how I go about accessing the value of this textbox.

Using: foreach(RepeaterItem item in rptCategories.Items)
I can loop through the parent, but how do I get the child information?

Anyone got any ideas?

Andyh

I’m going to assume the text box is in the items of the child repeater, this should do it, The C# might not be perfect tho…


Repeater objChildRepeater;
TextBox objTxtBox;
foreach (RepeaterItem objItem in Repeater1.Items) {
 objChildRepeater = ((Repeater)(objItem.FindControl("ctrlName")));
 if (!(objChildItem == null)) {
   foreach (RepeaterItem objChildItem in objChildRepeater.Items) {
	 objTxtBox = ((TextBox)(objChildItem.FindControl("ctrlName")));
	 if (!(objTxtBox == null)) {


	 }
   }
 }
}


Hope that helps

:slight_smile:

Cheers dhtmlgod

That works great!

One problem though, when I try to get the text value of the particular text box, it is either null or “”!

Any ideas why this is?

usually it is something on the lines of:
string xxx = objTxtBox.Text;
isn’t it?

Am i missing something!

Cheers
Andy

It depends where you are trying this. I assume you’re putting a value of some kind in the text box, but where are you calling this code? If it is on a postback, is viewstate enabled for the Repeater control? Otherwise, have you called DataBind() on the control before iterating through its items?

Cheers devbanana

I have sorted one bit out - I can now retrieve the value of my textbox. The problem was that I had forgotten to add the !IsPostBack and hence the data was lost.

I do have another problem though, and that is -

How do i get the relevant information back from the dataset for each individual child element???

Well, assuming the textbox contains some value corresponding with a column in the table, just do the appropriate select query or pass the value to a stored procedure if you have one for this purpose. If the data is already in a detached dataset, then try row filtering on a data view.

I would recommened this method to save extra trips to the database. Well, no, I would suggest using a DataTable, not a dataset.

[rant]
Never never never never never never never use a DataSet in a web application unless you really really really have to. If you think you have to, come see me and I’ll show you how you DON’T have to!
[/rant]

No, the textbox is not bound to any data - it is simply there to receive an integer value so that I can then update the dtaabase based on this value and the ID of the child element it is associated with.

Does that make sense???

Then you should have a stored procedure which will accept this integer as an input parameter.

Lol, I’ll agree with dhtmlgod in most cases. :slight_smile: There are so many directions you can go when it comes to data access in ASP.NET, including datasets, typed datasets, custom entities, etc…I guess it’s up to you to weigh the pros and cons and come up with your own opinion on the matter.

:nono: I’ll take this outside later :stuck_out_tongue:

You could have a label (hidden if you want) with the ID in it for each item. not the most elegant, but it’s simple and saves any extra round trips to the database.

So:
How do i get the relevant information back from the dataset for each individual child element???

i.e The ProductID (in this case) so that I can pass this AND the integer value into my stored procedure?

yeah, that would work, but it is not very clean?

It’s not too bad, just doesn’t sit well with me, a personal preference.

Know what the code is you need?

No!!!

objLabel = ((Label)(objChildItem.FindControl(“lblProduct”)));

???

I’m leaving for work, I’m sure dev will help ya, if not, I’ll get to you in a few.

:slight_smile:

no probs, I am off now anyway!

Cheers

Uhoh, I shall feel the wrath of dhtmlgod :eek:
I never said I liked them, just that they are there. I prefer custom business entities, however much of a pain they are to develop :frowning:
May I ask the mercy of dhtmlgod? :good:

Anyway, back to the topic at hand…

I’m assuming that with each TextBox, you have a [url=http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.button(VS.80,d=ide).aspx]Button to update that row? I don’t know the structure of your [url=http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(VS.80,d=ide).aspx]Repeater controls, so I’ll go on what would make sense.

If so, you’d want to pass the unique ID in the CommandArgument of the button, pass some value as the [url=http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname(d=ide).aspx]CommandName, and handle the [url=http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand(VS.80,d=ide).aspx]ItemCommand event.

You’d get the ID out of RepeaterCommandEventArgs.CommandArgument, get the TextBox value by finding it within [url=http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeatercommandeventargs.item.aspx]RepeaterCommandEventArgs.Item, and then update the database accordingly.

But yes, you would want a stored procedure in the database for this task, as I don’t like using queries directly in code when I don’t have to. :slight_smile: