Newbie!!!Cannot retrieve selectedvalue of a dynamically created dropdown on postback

A

AlecL

Please help!!!

I am creating dropdownlists, the number of which and its listitem is
determined on the value of other controls. The dropdowns are created
fine in a place holder, but when I go to retrieve the selectedvalue
like this:

sMentChildGender = CType(Page.FindControl("ddlMentChildGender" &
childcount), DropDownList).SelectedValue
sMentChildAge = CType(Page.FindControl("ddlMentChildAge" &
childcount), DropDownList).SelectedValue

it gives me a "System.NullReferenceException: Object reference not set
to an instance of an object" error. These dropdowns has to be created
as the result of the value of other control so they are created in a
SelectedIndexChanged code. Is that correct?

PLease help!!!!

Al
 
B

bruce barker

they need to be recreated in oninit of the postback. store some state
information is session or view state so you can recreate them.

-- bruce (sqlwork.com)
 
A

AlecL

they need to be recreated in oninit of the postback. store some state
information is session or view state so you can recreate them.

-- bruce (sqlwork.com)









- Show quoted text -

Thanks for your reply Bruce!
Will it retain the value of OnSelectedValue when recreated?
 
L

Lloyd Sheen

AlecL said:
Thanks for your reply Bruce!
Will it retain the value of OnSelectedValue when recreated?

Yes, the create in the page init will create the objects and the viewstate
will be applied so that it will be handled exactly as if the controls were
created statically.

LS
 
A

AlecL

Yes, the create in the page init will create the objects and the viewstate
will be applied so that it will be handled exactly as if the controls were
created statically.

LS- Hide quoted text -

- Show quoted text -

I put the same code that I used to create the dropdownlists in the on
selected event and it is still giving me the 'Object reference not set
to an instance of an object' error. Any ideas?
 
L

Lloyd Sheen

AlecL said:
I put the same code that I used to create the dropdownlists in the on
selected event and it is still giving me the 'Object reference not set
to an instance of an object' error. Any ideas?

If you don't create the controls on the init they will not be available and
will not have their viewstate renewed. Then the object will not be present
when your code executes.

LS
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top