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

  • Thread starter Thread starter AlecL
  • Start date Start date
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
 
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)
 
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?
 
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
 
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?
 
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
 
Back
Top