accessing datalist in repeater

J

jjack100

I have a form that is submitted to another page. The form contains a
variable number of dropdownlists within a repeater. When the form is
submitted, I need to access the values of each dropdownlist. I never
know how many dropdownlists will be present in advance.

Also, both the repeater and the dropdownlists are nested within other
controls:

ctl00$ContentPlaceHolder1$rptOptions$ctl01$dlOptions

So how can I access the dropdownlists? I am using PreviousPageType on
the page that the form is being posted to.

I thought it would be something like this:

foreach (RepeaterItem item in rptOptions.Items)
{
DropDownList b = (DropDownList)item.FindControl("dlOptions");
Response.Write(b.Text + "<br>");
}

But I just can't get the syntax right.

Thanks

Jack
 
G

Guest

Try

foreach (RepeaterItem item in rptOptions.Items)
{
DropDownList b = (DropDownList)item.FindControl("dlOptions");
Response.Write(b.SelectedItem.Text + "<br>");
}

HTH


Elton Wang
 
J

jjack100

I guess the real issue is that I don't know the correct syntax to
access the repeater on the second page:

The trace of the form contents shows this:

ctl00$ContentPlaceHolder1$rptOptions$ctl01$dlOptions 4
ctl00$ContentPlaceHolder1$rptOptions$ctl02$dlOptions 1

So the values are getting through. I tried this:

Repeater rpt =
(Repeater)Page.PreviousPage.FindControl("ctl00").FindControl("ContentPlaceHolder1").FindControl("dlOptions");

But I just get "Object reference not set to an instance of an object."

So how can I access those repeaters? Also, as I said before, the number
of repeaters varies.

Thanks
 
J

jjack100

I am now able to access the values when I do this:

Repeater rpt =
(Repeater)Page.PreviousPage.FindControl("ctl00").FindControl("ContentPlaceHolder1").FindControl("rptOptions");

DropDownList ddl =
(DropDownList)rpt.FindControl("ctl01").FindControl("dlOptions");
Response.Write("Here it is:" + ddl.SelectedItem.Value.ToString());

However, when I try to do this:

foreach (RepeaterItem item in rpt)
{
DropDownList b = (DropDownList)item.FindControl("dlOptions");
Response.Write(ddl.SelectedItem.Value.ToString()); + "<br>");
}

it chokes. Any ideas?

Thanks
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top