Getting data out of the Repeater Web Control

G

Guest

Hello -

I'm using a Repeater control to render information in a very customized
grid-like table. The Repeater control is binded to a DataSet with several
records of information. Within the Repeater control, I've placed
DropDownLists and CheckBoxes.

When the user has updated the information, he/he clicks the submit button
which is outside the scope of the Repeater control.

What i need to do, is read each of the values from DropDownLists and
CheckBoxes and save these into the database. How can i read these controls
from code-behind?

I was thinking of using:

foreach (Control ctr in Page.Controls)
{
if (ctr is DropDownList)
{
// do something
}
}

However, i cannot find the repeater control. Any ideas how to do this?

Thanks,
 
G

Guest

Thanks, but in my scenario, this is not possible. My custom-grid created with
the repeater controls, comprises, several different DataSets as well as
unique info from constants, session vars, etc... Hence, i need to be able to
loop through the controls collection. Any ideas?

Thanks again,
 
E

Eliyahu Goldin

You can loop through the repeater Items collection, find the ddls and
checkboxes with the item's FindControl method, get the values, set them in
the dataset table and at the end call the Update method of the data adapter
that links the dataset to the database.

Eliyahu
 
G

Guest

First, thanks Eliyahu for your help.

I don't have nested repeaters, i am only using one. I do have other nested
controls, specifically the DropDownList and CheckBox. The Submit Button is
not part of the Repeater control.

I am assuming that you suggest taht i use the Repeater.Controls collection
to loop?
 
E

Eliyahu Goldin

Your original question refers to just one repeater. Do you mean you have
nested repeaters? You can always get to any single one of them and then loop
through its items.

Eliyahu
 
G

Guest

Hi Charliewest,

loop through the repeater.items collection
HTH jd

For Each item As RepeaterItem In rep.Items

If item.ItemIndex > -1 Then

Dim myVal As String

' this assumes you do not know the id of the control
' if you do use then item.FindControl(controlId)
' also if the repeateritem contains more than one checkbox
' or more than one dropdown list you will be left with the
value of the
' last control examined.... but you should get the idea...

For Each con As Control In item.Controls

If TypeOf con Is DropDownList Then

myVal = CType(con, DropDownList).SelectedValue

ElseIf TypeOf con Is CheckBox Then

If CType(con, CheckBox).Checked Then

myVal = CType(con, CheckBox).Text

End If

Else

'do nothing

End If
Next
End If

Next
 
G

Guest

This is perfect. Thanks. My code in C# for those who might be interested is:

// Get Data out of Repeater Control
System.Collections.ArrayList ar_Status = new ArrayList();
System.Collections.ArrayList ar_CheckBox = new ArrayList();
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemIndex > -1)
{
foreach (Control ctr in item.Controls)
{
if (ctr is DropDownList)
{
ar_Status.Add(((DropDownList)ctr).SelectedValue);
}
else if (ctr is CheckBox)
{
ar_CheckBox.Add(((CheckBox)ctr).Checked);
}
}
}
}
 
G

Guest

Did that soultion work for you? I am stuck on the DropDownList being cleared
out on PostBack because the ItemCreated event isnt being called after the
original databind. but if I do the databind then I lose all of the users
changes.

Any ideas?
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top