Composite Control question

  • Thread starter ~~~ .NET Ed ~~~
  • Start date
N

~~~ .NET Ed ~~~

Background:
My (web) composite control has several drop down lists and buttons..
These drop downs get their data from the database (the drop downs are
DataBind'ed). And these are kind of cascaded (2nd depends on the selection
of the 1st).

Issue:
When I select an item from the dropdownlist control the
SelectedIndexChanged event gets fired, there is a PostBack then. At this
point I notice that CreateChildControls() is being called again for my
composite control. Does that mean that the control gets re-instantiated on
every postback? I was under the impression that once created the instance
"lived" on the server until the page was unloaded.

Yes, I am sure it is a pretty stupid question but I would like to know if I
misunderstood something. In particular I wanted to determine within my
composite if it was in a PostBack or not. From my previous post it is clear
that Page.IsPostBack is not available in CreateChildControls()
 
S

Steve C. Orr [MVP, MCSD]

Every control on a page must be re-created every time the page is posted
back.
With the built in web controls placed at design time, it all happens
automatically so you might not notice.
But for custom controls (and controls instantiated an runtime) it is a bit
more of a manual process so you need to be aware.
 
R

Riki

~~~ .NET Ed ~~~ said:
Background:
My (web) composite control has several drop down lists and
buttons.. These drop downs get their data from the database (the drop
downs are DataBind'ed). And these are kind of cascaded (2nd depends
on the selection of the 1st).

Issue:
When I select an item from the dropdownlist control the
SelectedIndexChanged event gets fired, there is a PostBack then. At
this point I notice that CreateChildControls() is being called again
for my composite control. Does that mean that the control gets
re-instantiated on every postback?
Yes

I was under the impression that
once created the instance "lived" on the server until the page was
unloaded.

The page is unloaded as soon as it is sent to the client.
After postback, a new page is built.
Yes, I am sure it is a pretty stupid question but I would like to
know if I misunderstood something.

There are no stupid questions here Ed.
In particular I wanted to
determine within my composite if it was in a PostBack or not. From my
previous post it is clear that Page.IsPostBack is not available in
CreateChildControls()

Typically, you check the Viewstate of your control to find out
if it is in a postback. You also use the viewstate to rebuild
your control (instead of databinding in your case),
allowing postback data to be preserved.

Something like this (C#):

protected override void CreateChildControls()
{
Controls.Clear ();
if (ViewState[ITEMCOUNT] != null) {
// postback scenario
// create child control tree from viewstate
} else {
// create child control tree (first time)
}
}

I don't know a good online sample for you, but maybe
Mono's Repeater could inspire you:
http://svn.myrealbox.com/source/trunk/mcs/class/System.Web/System.Web.UI.WebControls/Repeater.cs
You'll have to ignore all the databinding and templating
code though, because that part is not relevant to you.
But the ViewState part should be interesting.
 

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,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top