server control: recreating controls after roundtrip

J

J.Hero

How can i recreate controls which were added to the control collection on
runtime after a roundtrip in a server control with no aspx/ascx files? There
is no page object in which i could ask for the postback status.

Any ideas?

Thanks for your help.

J.Hero
 
N

Nenad Prekupec

that kind of stuff should be done in CreateChildControls override method.
I think that you don't have to look for postback event in server control as
all things attached to that control are being recreated.
The only problem I think you have is that you are filling your collection on
wrong place, as after post back you should have all items in collection and
created and rendered in control no metter what. You should check if all
controls you have added to collection exist in time Render method is
invoked, if not you should check at that place where you are adding your
runtime items to collection once again.

Nenad Prekupec
 
J

J.Hero

Do you know where I can find a good tutorial or a small sample code? I
understood the principal but I don't see a way how to check if one of the
controls was created before the last roundtrip or not.
 
T

Teemu Keiski

When calling EnsureChildControls() you can make sure that child controls
(specified in CreateChildControls) are created when needed.
 
J

J.Hero

How can ensurechildcontrols help? I have different "stages" of controls. Not
all child controls should be createt at the same time. Small sample code:
(Sorry but perhaps my biggest problem is that I couldn't describe my problem
with the right words.)

public class Test_Control : WebControl
{
Button but1 = new Button();
Button but2 = new Button();
Button but3 = new Button();

protected override void CreateChildControls()
{
this.FirstControl(this, new EventArgs());
}

private void FirstControl(object sender, System.EventArgs e)
{
this.but1.Text = "Button 1";
this.but1.EnableViewState = true;
this.but1.ID = "but1";
this.but1.Click += new EventHandler(this.SecondControl);
Controls.Add(but1);
}

private void SecondControl(object sender, System.EventArgs e)
{
this.but2.Text = "Button 2";
this.but2.EnableViewState = true;
this.but2.ID = "but2";
this.but2.Click += new EventHandler(this.ThirdControl);
Controls.Add(but2);
}

private void ThirdControl(object sender, System.EventArgs e)
{
this.but3.Text = "Button 3";
this.but3.EnableViewState = true;
this.but3.ID = "but3";
Controls.Add(but3);
}
}

At the beginning only Button1 should be created. Button1 should create
Button2. Works fine. Button2 should create Button3. But only Button1 is
createt (from createchildcontrols). So i need to know which controls where
in the list before the roundtrip.
 

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

Latest Threads

Top