dropdownlist empty after submit

X

xke

I give up after spending some time with this issue.

I have a text, a dropdown and a submit button on my webpage (net
2005).
Type smth in Textbox, pick up a value on drpdwn list and submit.
Textbox shows the typed value but the drpdwn loses the selection ...
actually it's empty.
Values for drpdown list are coming on load if not IsPostBack.

If I fill the drpdwn list like
<asp:dropdownlist id="dd" runat="server">
<asp:ListItem Text="1" value="1"></asp:ListItem>
<asp:ListItem Text="2" value="2"></asp:ListItem>
</asp:dropdownlist>

Then everything is perfect.

I must say I do have a BasePage and the control which contains the
drpdwn is part of a page which inherits BasePage class.
Tha BasePage class overrides CreateChildControls and Render methods.

Why can cause drpdwn to lose its state?
Thousand Thanks,
xkeops
 
M

Mark Rae [MVP]

Values for drpdown list are coming on load if not IsPostBack.

And there's your problem.

Populate the DropDownList in Page_Init instead of Page_Load and all will be
well...
 
L

Liz

Populate the DropDownList in Page_Init instead of Page_Load and all will
be well...

to set up a Page_Init handler do you have to type in the handler method code
or does the IDE have a facility to generate it as it does for Page_Load when
you double-click on an empty area of the page ? or when you select the
event from a list of page events in a property box and double-click *that* ?
I don't have any problem with typing in the handler but I would like to
understand the VS 2005 environment and what it can and can't do ..

TIA

L
 
X

xke

Hi Mark,

You are perfectly right, I populate the drpdwn on Page_Init and it
works. Thanks.
Could you offer some more explanation, it's excellent that it's
working but I'd like to knowhy.

Thanks again,
xk
 
M

Mark Rae [MVP]

to set up a Page_Init handler do you have to type in the handler method
code or does the IDE have a facility to generate it as it does for
Page_Load when you double-click on an empty area of the page ? or when
you select the event from a list of page events in a property box and
double-click *that* ? I don't have any problem with typing in the handler
but I would like to understand the VS 2005 environment and what it can and
can't do ..

I don't know, to be perfectly honest... I've always just typed it
manually...
 
M

Mark Rae [MVP]

Could you offer some more explanation, it's excellent that it's
working but I'd like to knowhy.

It's to do with the ASP.NET page life cycle:
http://msdn2.microsoft.com/en-us/library/ms178472(vs.80).aspx

Things happen in a set order and, generally speaking, populating
DropDownLists in Page_Load is too far down the cycle as they then risk being
overwritten by ViewState.

It's a little more complex than that, but that's the general idea...
 

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,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top