By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,081 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,081 IT Pros & Developers. It's quick & easy.

Viewstate Dynamically added control (ASP.NET 2.0)

P: n/a
Hello,

I have a problem in ASP.NET 2.0 with the viewstate of my dynamically
added user control. I have reproduced the problem with a very simple
user control and a very simple page.

On my usercontrol is a button and a label. Everytime the button is
clicked a counter which is stored in the viewstate is increased and
displayed in the label.

The code looks something like this:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. btnID.Click += new EventHandler(btnID_Click);
  4. }
  5.  
  6. void btnID_Click(object sender, EventArgs e)
  7. {
  8. this.Counter+= 1;
  9. lblAdd.Text = this.Counter.ToString();
  10. }
  11.  
  12. private int Counter
  13. {
  14. get { return this.ViewState["counter"] == null ? 0 :
  15. (int)this.ViewState["counter"]; }
  16. set { this.ViewState.Add("counter", value); }
  17. }
  18.  
On my page is a button. In the event handler of the click event my
user control is loaded and added to a placeholder on my page. When a
postback happens after this, the usercontrol is loaded again in the
Page_Load. This works without any problems: the usercontrol is loaded
and the events of the button on the user control work fine. The code
looks something like this:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. btnAddControl.Click += new EventHandler(btnAddControl_Click);
  4.  
  5. if (this.Init)
  6. {
  7. Test ctrl = this.LoadControl("~/Test.ascx") as Test;
  8. ctrl.ID = "TEST";
  9. plcControl.Controls.Add(ctrl);
  10. }
  11. }
  12.  
  13. void btnAddControl_Click(object sender, EventArgs e)
  14. {
  15. Test ctrl = this.LoadControl("~/Test.ascx") as Test;
  16. ctrl.ID = "TEST";
  17. plcControl.Controls.Add(ctrl);
  18. this.Init = true;
  19. }
  20.  
  21. private bool Init
  22. {
  23. get { return this.ViewState["init"] == null ? false :
  24. (bool)this.ViewState["init"]; }
  25. set { this.ViewState.Add("init", value); }
  26. }
  27.  
However when I try to initialize my counter with a number after the
load, the value is not saved in the viewstate of the user control. I
added a line to the event handler of the btnAddControl, so it looks
like this:

Expand|Select|Wrap|Line Numbers
  1. void btnAddControl_Click(object sender, EventArgs e)
  2. {
  3. Test ctrl = this.LoadControl("~/Test.ascx") as Test;
  4. ctrl.ID = "TEST";
  5. ctrl.Counter = 20;
  6. plcControl.Controls.Add(ctrl);
  7. this.Init = true;
  8. }
  9.  
  10.  
When I click the button on the usercontrol the first time the counter
is not in the viewstate and the counter starts from 0. However when I
add the following line to the Page_Load of the usercontrol, the
Counter is initialized correctly to 20:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. this.Counter = this.Counter;  //This is the new line!
  4. btnID.Click += new EventHandler(btnID_Click);
  5. }
  6.  
I've read the page lifecycle articles, but I can't see the problem.
The value 20 of the initialization is in the viewstate, but it is not
saved unless I added the last line the the Page_Load event.

Can anyone help me with this problem or explain why the value is not
saved in the viewstate? Any thoughts on adding user controls
dynamically to a page are appreciated as well.

Kind regards,
Jelle

Apr 25 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You're adding "the" Control twice. You're not adding the same instance of a
Control; you're overwriting the first instance the second time you call
"LoadControl." Note that the LoadControl method takes a path to the .ascx
file, but that a Control is an instance of a class. Therefore, when you call
LoadControl, you are creating an instance of the Control's class. Also, here
are the remarks from the documentation regarding the
TemplateControl.LoadControl method:

"When you load a control into a container control, the container raises all
of the added control's events until it has caught up to the current event.
However, the added control does not catch up with postback data processing.
For an added control to participate in postback data processing, including
validation, the control must be added in the Init event rather than in the
Load event."

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<je**********@gmail.comwrote in message
news:11**********************@b40g2000prd.googlegr oups.com...
Hello,

I have a problem in ASP.NET 2.0 with the viewstate of my dynamically
added user control. I have reproduced the problem with a very simple
user control and a very simple page.

On my usercontrol is a button and a label. Everytime the button is
clicked a counter which is stored in the viewstate is increased and
displayed in the label.

The code looks something like this:

Expand|Select|Wrap|Line Numbers
  1.    protected void Page_Load(object sender, EventArgs e)
  2.    {
  3.        btnID.Click += new EventHandler(btnID_Click);
  4.    }
  5.    void btnID_Click(object sender, EventArgs e)
  6.    {
  7.        this.Counter+= 1;
  8.        lblAdd.Text = this.Counter.ToString();
  9.    }
  10.    private int Counter
  11.    {
  12.        get { return this.ViewState["counter"] == null ? 0 :
  13. (int)this.ViewState["counter"]; }
  14.        set { this.ViewState.Add("counter", value); }
  15.    }
  16.  

On my page is a button. In the event handler of the click event my
user control is loaded and added to a placeholder on my page. When a
postback happens after this, the usercontrol is loaded again in the
Page_Load. This works without any problems: the usercontrol is loaded
and the events of the button on the user control work fine. The code
looks something like this:

Expand|Select|Wrap|Line Numbers
  1.    protected void Page_Load(object sender, EventArgs e)
  2.    {
  3.        btnAddControl.Click += new EventHandler(btnAddControl_Click);
  4.        if (this.Init)
  5.        {
  6.            Test ctrl = this.LoadControl("~/Test.ascx") as Test;
  7.            ctrl.ID = "TEST";
  8.            plcControl.Controls.Add(ctrl);
  9.        }
  10.    }
  11.    void btnAddControl_Click(object sender, EventArgs e)
  12.    {
  13.        Test ctrl = this.LoadControl("~/Test.ascx") as Test;
  14.        ctrl.ID = "TEST";
  15.        plcControl.Controls.Add(ctrl);
  16.        this.Init = true;
  17.    }
  18.    private bool Init
  19.    {
  20.        get { return this.ViewState["init"] == null ? false :
  21. (bool)this.ViewState["init"]; }
  22.        set { this.ViewState.Add("init", value); }
  23.    }
  24.  

However when I try to initialize my counter with a number after the
load, the value is not saved in the viewstate of the user control. I
added a line to the event handler of the btnAddControl, so it looks
like this:

Expand|Select|Wrap|Line Numbers
  1.    void btnAddControl_Click(object sender, EventArgs e)
  2.    {
  3.        Test ctrl = this.LoadControl("~/Test.ascx") as Test;
  4.        ctrl.ID = "TEST";
  5.        ctrl.Counter = 20;
  6.        plcControl.Controls.Add(ctrl);
  7.        this.Init = true;
  8.    }
  9.  

When I click the button on the usercontrol the first time the counter
is not in the viewstate and the counter starts from 0. However when I
add the following line to the Page_Load of the usercontrol, the
Counter is initialized correctly to 20:

Expand|Select|Wrap|Line Numbers
  1.    protected void Page_Load(object sender, EventArgs e)
  2.    {
  3.        this.Counter = this.Counter;  //This is the new line!
  4.        btnID.Click += new EventHandler(btnID_Click);
  5.    }
  6.  

I've read the page lifecycle articles, but I can't see the problem.
The value 20 of the initialization is in the viewstate, but it is not
saved unless I added the last line the the Page_Load event.

Can anyone help me with this problem or explain why the value is not
saved in the viewstate? Any thoughts on adding user controls
dynamically to a page are appreciated as well.

Kind regards,
Jelle

Apr 25 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.