473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Viewstate Dynamically added control (ASP.NET 2.0)

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
1 7585
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eamonn O'Connell | last post by:
Hi, We have an ASP.net application, where we dynamically add controls, in the Page_Load. Occasionally, when the user posts back the page (e.g. clicks Save button on the page), we get a "Failed...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
3
by: Kenton Smeltzer | last post by:
Hello All, I am having a problem with events and the addition of controls on a page I am developing. First let me tell you what I have tried and then maybe someone can see something I missed. ...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
6
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
2
by: Jarod | last post by:
Hey I change backColor of linkButton in my Page_Load: lbDoSth.BackColor = Color.Red; But I have multiView on this page. On one of the views I have detailsView. When I add a new row a set...
1
by: Andrew Robinson | last post by:
I have a <asp:table> control with a large number of dynamically created LinkButtons. I am using the command event, command name and command argument values in my LinkButtons to trigger actions...
0
by: nithagowda | last post by:
Hello, I have a DropDownList for source type and a panel for displaying TreeView based on the source type.. The problem is- if i select different source type drom ddlSrcType, other than the...
0
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.