Dynamic control creation

K

Kirill Osipov

Hello everybody!

Sorry if it's not the right newsgroup to post my message to. Anyways, any
advice is very helpful to me as I am an absolute novice in Web Forms.
Here is a small piece of code. It is actually a lightweight scenario of what
is going to happen in my project.
------------------------------------------------
namespace bluebook
{
public class Testpage : Page
{
protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder also resides on the
corresponding aspx page

private void Page_Load(object sender, System.EventArgs e)
{
LinkButton aLB = new LinkButton();
aLB.CommandName = "Command 1";
aLB.Text = "Level 1";
aLB.Command += new CommandEventHandler(OnCommand);

ctlPlaceHolder.Controls.Add(aLB);
this.SaveViewState();
}

private void OnCommand(object sender, CommandEventArgs args)
{
LinkButton aLB = (LinkButton) sender;
int a = Int32.Parse( aLB.CommandName[8].ToString() );
a++;

LinkButton aNewLB = new LinkButton();
aNewLB.CommandName = "Command " + a.ToString();
aNewLB.Text = "Level " + a.ToString();

ctlPlaceHolder.Controls.Clear();
ctlPlaceHolder.Controls.Add(aNewLB);
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
------------------------------------------------

What I was expecting is that every time I click the hyperlink, the form is
regenerated and the text inside the hyperlink is incremented. So first it
displays Level 1, then Level 2, then Level 3, ..., Level 10, Level 2, ...
and so on. But instead it only increments once, and then goes back to the
initial state.
I realize that changing the hyperlink's text can be achieved without
creating a new control each time an event is raised. But as I have already
said, this is just a sample, and in the real project I am working on I DO
NEED to create the control dynamically each time I process the event.
Does anyone know a workaround? Or, maybe, it is a bug in the technology?
Regards,
Kirill Osipov
 
A

Alex

Have you tried adding the
if(!IsPostBack){
//put pageload code here
}
to your Page_Load function?
 
K

Kirill Osipov

Yes, I did, but it didn't help

Alex said:
Have you tried adding the
if(!IsPostBack){
//put pageload code here
}
to your Page_Load function?

-----Original Message-----
Hello everybody!

Sorry if it's not the right newsgroup to post my message to. Anyways, any
advice is very helpful to me as I am an absolute novice in Web Forms.
Here is a small piece of code. It is actually a lightweight scenario of what
is going to happen in my project.
------------------------------------------------
namespace bluebook
{
public class Testpage : Page
{
protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder also resides on the
corresponding aspx page

private void Page_Load(object sender, System.EventArgs e)
{
LinkButton aLB = new LinkButton();
aLB.CommandName = "Command 1";
aLB.Text = "Level 1";
aLB.Command += new CommandEventHandler(OnCommand);

ctlPlaceHolder.Controls.Add(aLB);
this.SaveViewState();
}

private void OnCommand(object sender, CommandEventArgs args)
{
LinkButton aLB = (LinkButton) sender;
int a = Int32.Parse( aLB.CommandName[8].ToString() );
a++;

LinkButton aNewLB = new LinkButton();
aNewLB.CommandName = "Command " + a.ToString();
aNewLB.Text = "Level " + a.ToString();

ctlPlaceHolder.Controls.Clear();
ctlPlaceHolder.Controls.Add(aNewLB);
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
------------------------------------------------

What I was expecting is that every time I click the hyperlink, the form is
regenerated and the text inside the hyperlink is incremented. So first it
displays Level 1, then Level 2, then Level 3, ..., Level 10, Level 2, ...
and so on. But instead it only increments once, and then goes back to the
initial state.
I realize that changing the hyperlink's text can be achieved without
creating a new control each time an event is raised. But as I have already
said, this is just a sample, and in the real project I am working on I DO
NEED to create the control dynamically each time I process the event.
Does anyone know a workaround? Or, maybe, it is a bug in the technology?
Regards,
Kirill Osipov


.
 
E

Ezra Epstein

I do know that the one of the short-comings of ASP.NET is that it doesn't
handle non-declarative (i.e., dynamic) stuff very well.

The key problem seems to be the way that ViewState was implemented.

For your control to work several things need to happen:

1. The control must be added before the call to SaveViewState in the
parent control (e.g., the page). Doing this at Page_Load as you have done
should work.
2. on post-back the control MUST be re-added before LoadViewState is
called. I.e., way before Page_Load. You can override OnInit() to handle
this. However, the VS designer likes to override this method so you might
not be able to use VS's dynamic layout tools on the page in question.
3. You must ensure that the control participates in the ViewState
methods. I'm still looking for documentation on that one.
4. Finally, when the control is rendered it must generate the call to
postback on the client (do a view page source in your browser to see how a
regular control does this). You *can* do this manually, but there should be
a way to have the control do this.

The ASP,Net model is great for the visual version of a "power" developer:
drag & drop whooo-wheee. It's actually really great so long as you're
clearly within the bounds that the MS engineers defined as being the problem
space. The trouble comes when you step across those bounds. (a) it seems
they didn't design it to handle truly dynamic content creation (the real
power developer) and they didn't document it too well either.... It's a
bummer. As you know the .aspx or .ascx is a sub-class of the code-behind.
This subclass is hooked up to the page rendering mechanism ... trouble is
figuring out the non-documented parts of that mechanism.

Good luck, hope this starts. Whatever else you discover, please share.

Ezra e.


Kirill Osipov said:
Hello everybody!

Sorry if it's not the right newsgroup to post my message to. Anyways, any
advice is very helpful to me as I am an absolute novice in Web Forms.
Here is a small piece of code. It is actually a lightweight scenario of what
is going to happen in my project.
------------------------------------------------
namespace bluebook
{
public class Testpage : Page
{
protected PlaceHolder ctlPlaceHolder; //ctlPlaceHolder also resides on the
corresponding aspx page

private void Page_Load(object sender, System.EventArgs e)
{
LinkButton aLB = new LinkButton();
aLB.CommandName = "Command 1";
aLB.Text = "Level 1";
aLB.Command += new CommandEventHandler(OnCommand);

ctlPlaceHolder.Controls.Add(aLB);
this.SaveViewState();
}

private void OnCommand(object sender, CommandEventArgs args)
{
LinkButton aLB = (LinkButton) sender;
int a = Int32.Parse( aLB.CommandName[8].ToString() );
a++;

LinkButton aNewLB = new LinkButton();
aNewLB.CommandName = "Command " + a.ToString();
aNewLB.Text = "Level " + a.ToString();

ctlPlaceHolder.Controls.Clear();
ctlPlaceHolder.Controls.Add(aNewLB);
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
------------------------------------------------

What I was expecting is that every time I click the hyperlink, the form is
regenerated and the text inside the hyperlink is incremented. So first it
displays Level 1, then Level 2, then Level 3, ..., Level 10, Level 2, ...
and so on. But instead it only increments once, and then goes back to the
initial state.
I realize that changing the hyperlink's text can be achieved without
creating a new control each time an event is raised. But as I have already
said, this is just a sample, and in the real project I am working on I DO
NEED to create the control dynamically each time I process the event.
Does anyone know a workaround? Or, maybe, it is a bug in the technology?
Regards,
Kirill Osipov
 

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,822
Latest member
israfaceZa

Latest Threads

Top