derived control doesn't maintain state

B

buga

Hi
I've made my own DropDownList by deriving the WebControls.DropDownList control. So far, all I did was to override the Render procedure and fill the control with some pair of values/strings

Protected Overrides Sub Render(ByVal html As HtmlTextWriter
MyBase.Items.Add(New ListItem("something",somevalue
..
MyBase.Render(html
End Su

However, unlike the base control, my derived dropdownlist doesn't maintain state after postback, so I can't access properties like selecteditem

What must I do?
 
T

Teemu Keiski

Hi,

Render is too late stage for this (in control lifecycle). Postback data
handling, when selected items and such are detected & selected, happens
before just before Load and second try just after Load.

About control lifecycle:
http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.asp

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke


Hi,
I've made my own DropDownList by deriving the WebControls.DropDownList
control. So far, all I did was to override the Render procedure and fill the
control with some pair of values/strings:

Protected Overrides Sub Render(ByVal html As HtmlTextWriter)
MyBase.Items.Add(New ListItem("something",somevalue)
...
MyBase.Render(html)
End Sub

However, unlike the base control, my derived dropdownlist doesn't maintain
state after postback, so I can't access properties like selecteditem.

What must I do?
 
F

Fred Hirschfeld

So you should override the OnPreRender method to add your extra items...

Fred
 
S

Sam

You should add the items either in Init() or in OnLoad(), as the
previous poster said. The reason is upon form submission the
framework will take the submitted value of the list item in the form
collection and look through the drop down control's list collection to
set the SelectedItem property. You better have those items in the
collection at that time so the framework can make a match!! This step
either occurs between Init and Load or just after Load and is called
ProcessPostData.

This is the most accurate link I have found so far to understand
control lifecycle:

http://aspalliance.com/articleViewer.aspx?aId=134&pId=

Forget Microsoft's link, it has many omissions. I hope this was
clear.

-Sam
 
T

Teemu Keiski

Adding ListItems to the ListItemCollection (Items property) is fine even if
it is done in OnPrerender at the first, non-postback request (I was unclear
with my first post), though depending on usage logic they might need to be
there in Load. ListItems are stored to ViewState completely and therefore at
postback also restored from there before postback processing occurs
(DropDownList posts only the selection and other /all items are actually
persisted via ViewState).

Why it didn't work with Render is that ViewState is saved before control is
rendered and therefore at postback ListItems weren't there anymore.
OnPreRender happens before saving ViewState, and therefore adding items at
that stage can "make it".

Here is collected link set related to control/page lifecycle:
http://weblogs.asp.net/eporter/archive/2003/07/15/10109.aspx

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

http://blogs.aspadvice.com/joteke
 
F

Fred Hirschfeld

When you added the values in the Load, did you use DataBind() method and
then add the additional ones to your control? I use data binding in the Load
method but if you are mixing binding and manually added items, then the
manual added items must be done after the .DataBind().

You can add or insert into the Items collection to get exact position if
needed.

Fred


buga said:
Actually, I had tried to put the code in the Load(), but at that fase it
appears the properties I defined (needed to fill the list) weren't
available. That's why I moved it to Render in the first place. Since Init()
is previous to Load(), i guess it wouldn't access the properties either.
 
B

buga

No, I didn't use DataBind(). I simply did something like

For i = Min to Max ' both Properties from the control tag in the asp
MyBase.Items.Add(New ListItem(i,i)
Nex

and it simply rendered the control with 1 item (0)
However, if I hardcoded the values in the For loop it would be correctly rendered.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top