selectedindex after dropdownlist is added to placeholder

M

Michael Tanner

I have created a custom control that derives from the PlaceHolder control.
Based on a property setting, I load a DropDownList or a TextBox into the
PlaceHolder control. I load the DropDownList or TextBox control in
protected override void OnPreRender(EventArgs e).

The issue that I have is getting the SelectedIndex from the DropDownList
after a postback. The PlaceHolder control does not have the SelectedIndex
property and I can't figure out how to maintain the DropDownList after a
postback - so FindControl doesn't work for me.

Any ideas would be appreciated.
 
V

Victor Garcia Aprea [MVP]

Hi Michael,

Note that adding the child controls at PreRender may be too late, as several
key event (Init, Load, viewstate loading,e tc) had already been fired and
your added control wasn't there at the proper time. If you're writing a
composite custom control then CreateChildControls is the place where to
create child controls.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
N

Nathanael Schulte

I'm having a similar problem on PostBack for a DropDownList.

I have a page called ManualAdd2.aspx
That page contains instances of two user controls:
HeaderBlock.ascx and ctlBrowse.ascx

HeaderBlock.ascx also contains an instance of ctlBrowse.ascx

ctlBrowse contains a DropDownList and a TextBox.

When I run ManualAdd2.aspx, the instance of ctlBrowse that is contained
within the HeaderBlock control holds state for the DropDownlist in it's
copy of ctlBrowse just fine.

The instances of ctlBrowse that just reside in ManualAdd2.aspx DO NOT
hold their state, however.

I've looked at the declarations of the control instances and cannot find
a single difference in declaration.

Any ideas?
 
M

Michael Tanner

Victor,

Thanks for you reply. I found CreateChildControl just a few hours after I
posted.

Thanks again,
Michael
 
N

Nathanael Schulte

Hey,

I had a similar issue to yours and maybe your problem is not the same,
but I'll share what fixed it for me anyway:

What I was finding was that on postback, my DropDownList would always to
back to the first entry, no matter what I picked. I pulled up the page
in the browser then went to View Source in IE. What I noticed was that
the Value for the rendered <OPTION> control was the same for every
entry. In other words, then I populated the DropDownList from a
database, the SelectedItem.Value was the same for every entry in the
drop down list, so it would always go back to the first entry in the
list and the SelectedIndexChanged event would never fire.
So, I would recommend making sure there is a unique value for every
entry in the DropDownList.

Hope this helps.

Thanks,
thanie2001
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top