Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamic control creation

Reply
Thread Tools

Dynamic control creation

 
 
Kirill Osipov
Guest
Posts: n/a
 
      09-11-2003
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


 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      09-12-2003
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
>
>
>.
>

 
Reply With Quote
 
 
 
 
Kirill Osipov
Guest
Posts: n/a
 
      09-12-2003
Yes, I did, but it didn't help

"Alex" <(E-Mail Removed)> wrote in message
news:084c01c378db$266dd910$(E-Mail Removed)...
> 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
> >
> >
> >.
> >



 
Reply With Quote
 
Ezra Epstein
Guest
Posts: n/a
 
      09-13-2003
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic creation of a user control with a dropdownlist Hugo Flores ASP .Net 1 09-06-2006 02:33 AM
Dynamic control creation design issue =?Utf-8?B?UGF1bA==?= ASP .Net 0 02-21-2006 07:45 AM
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM
Dynamic Control Creation and ViewState: what is the ASP.Net way? PABruceFan ASP .Net 3 07-31-2004 06:43 PM
Dynamic Control Creation w/Events? Jeff Voigt ASP .Net 1 07-08-2003 07:08 PM



Advertisments