Unable to get viewstate to work with dynamic controls...

T

Tom Wisnowski

Hello all,

Here is a simple example of what I'm doing...

Create a ASPX page
Add a PlaceHolder(plch) and a Button(btn) to the page.

In the code behind on Page_load add the following...
TextBox txt = new TextBox();
if(!IsPostBack)
{
txt.Text = "Hello";
txt.BackColor = Color.Yellow;
}
plch.Controls.Add(txt);

Run the page...

you will see the page renders the textbox correctly. Now
press the button to caused a postback. When the page is re-
rendered, the background color is cleared (it is never
loaded from the viewstate).

To test the viewstate issue, I created a new Custom
Control, MyTextBox, that inherits from TextBox. I
overrided the LoadViewState and SaveViewState methods to
add trace output so i could see when they fire. Here is
what i found...

When the page first loads, the control's SaveViewState is
called twice (Once right after the page's prerender and
once again in the page's SaveViewState, not sure why?).

On the first postback, the control's LoadViewState is
never called.

On Subsequent postbacks, the control's LoadViewState is
called right after the control is added to the
placeholder's controls collection.

Why is the first postback not calling the control's
LoadViewState? I assume this is the problem with my
example, the background color is lost because the first
postback never restores the object state, which clears the
background color.

I've been banging my head on this one for a while now and
ANY comments would be appreciated!

Thanks,

Tom Wisnowski, MCAD

I was also wondering...
I thought MSDN subscribers were suposed to get responses
posted back within 72 hours, But most of my posts go un-
answered. Is there a problem with my email alias?
 
S

Saravana [MVP]

You can easily solve your problem by adding your dynamic controls at
Page_Init.
 
T

Tom Wisnowski

I have tried creating the control at page_init and still
get the same result.

Also to add, i forgot to put the line in that sets my
textbox id in my sample code. My sample should be...

private void Page_Load()
{
TextBox txt = new TextBox();
txt.ID = "MyID";
if(!IsPostBack)
{
txt.Text = "Hello";
txt.BackColor = Color.Yellow;
}
plch.Controls.Add(txt);
}

Any suggestions?

Tom Wisnowski, MCAD
 
J

Jacob Yang [MSFT]

Hi Tom,

Based on my research and experience, the following article is useful to
you. Please refer to it carefully.

Retaining State for Dynamically Created Controls in ASP.NET applications
http://www.codeproject.com/aspnet/retainingstate.asp
"...
When dynamically adding controls to an ASP.NET page in runtime the object
references are lost at postback because they have no handle in the
codebehind. The values entered by the user is not available when accessing
these objects after postback, and they seem empty. This article describes
how to use ViewState to recreate and reinsert the postback values into the
objects to access their values.
..."

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jacob Yang [MSFT]

Hi Tom,

I have done more research regarding this issue. Please try the following
steps on your side.

1. Create a default C# web application.

2. Put a button on the web form.

3. Add the following code in the code behind file.
------------------------------------------------
...
namespace DynamicControl
{
/// <summary>
/// Summary description for viewstate.
/// </summary>
public class viewstate : System.Web.UI.Page
{
...
private TextBox txt;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

if(!IsPostBack)
{
txt.Text = "Hello";
txt.BackColor = Color.Yellow;
}


}

protected void Page_Init(object sender, EventArgs e)
{
txt = new TextBox ( );
txt.Text = "Welcome to ASP.NET";
txt.ID = "MyID";
txt.EnableViewState = true;

PlaceHolder1.Controls.Add ( txt );
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
Page.Init += new System.EventHandler(Page_Init);
}
#endregion
}
}

4. Build the web application and test this issue.

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Top