Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 161643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:14:58+00:00 2026-05-11T11:14:58+00:00

A ASP.NET page’s ViewState seems to have troubles keeping up with dynamically removed controls

  • 0

A ASP.NET page’s ViewState seems to have troubles keeping up with dynamically removed controls and the values in them.

Let’s take the following code as an example:

ASPX:

<form id='form1' runat='server'> <div>     <asp:Panel runat='server' ID='controls' /> </div> </form> 

CS:

protected void Page_Init(object sender, EventArgs e) {     Button b = new Button();     b.Text = 'Add';     b.Click +=new EventHandler(buttonOnClick);     form1.Controls.Add(b);     Button postback = new Button();     postback.Text = 'Postback';     form1.Controls.Add(postback); }  protected void Page_Load(object sender, EventArgs e) {     if (ViewState['controls'] != null) {         for (int i = 0; i < int.Parse(ViewState['controls'].ToString()); i++) {             controls.Controls.Add(new TextBox());             Button remove = new Button();             remove.Text = 'Remove';             remove.Click +=new EventHandler(removeOnClick);             controls.Controls.Add(remove);             controls.Controls.Add(new LiteralControl('<br />'));         }     } }  protected void removeOnClick(object sender, EventArgs e) {     Control s = sender as Control;     //A hacky way to remove the components around the button and the button itself     s.Parent.Controls.Remove(s.Parent.Controls[s.Parent.Controls.IndexOf(s) + 1]);     s.Parent.Controls.Remove(s.Parent.Controls[s.Parent.Controls.IndexOf(s) - 1]);     s.Parent.Controls.Remove(s.Parent.Controls[s.Parent.Controls.IndexOf(s)]);     ViewState['controls'] = (int.Parse(ViewState['controls'].ToString()) - 1).ToString(); }  protected void buttonOnClick(object sender, EventArgs e) {     if (ViewState['controls'] == null)         ViewState['controls'] = '1';     else         ViewState['controls'] = (int.Parse(ViewState['controls'].ToString()) + 1).ToString();     controls.Controls.Add(new TextBox()); } 

Then, let’s say you create 4 controls and insert the following values:

[ 1 ] [ 2 ] [ 3 ] [ 4 ] 

We want to delete the second control; after removing the second control the output is:

[ 1 ] [ 3 ] [ 4 ] 

which is what we want. Unfortunately, on a subsequent PostBack, the list becomes:

[ 1 ] [ ] [ 3 ] 

So, my question is, why is this happening? As far as I’ve read, ViewState should save the properties of the controls in relation to their indexes, not the actual controls.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:14:58+00:00Added an answer on May 11, 2026 at 11:14 am

    Couple things. Whether controls are loaded by their ID or Index depends on the ViewStateModeById attribute. By default it is false (meaning load by index).

    However, text boxes are handled differently. Their view state does not contain input value unless they are disabled or invisible. The text property gets overwritten by posted values using their IDs. Since you are not managing text box IDs, this is what happens.

    After you’ve added four controls, you have four text boxes: ctrl0, ctrl1, ctrl2, and ctrl3 with values 1, 2, 3, and 4 respectively.

    Next, you remove the ctrl1 box and client gets three boxes: ctrl0, ctrl2, and ctrl3 with according values. Now, when you do any postback, these three values get submitted in the form ctrl0=1&ctrl2=3&ctrl3=4.

    Then, on Page_Load, you create three controls, this time: ctrl0, ctrl1, ctrl2 with no values.

    The Framework calls LoadRecursive to load view states and then ProcessPostData to assign input values. It sees submitted ctrl0 and ctrl2, finds controls with the same id and assigns them values 1 and 3. It does not find ctrl3, so it skips it. The remaining ctrl1 simply carries on w/o any value.

    As an example, consider this solution (not the best):

    protected void Page_Init(object sender, EventArgs e) {     Button b = new Button();     b.Text = 'Add';     b.Click += new EventHandler(buttonOnClick);     form1.Controls.Add(b);      Button postback = new Button();     postback.Text = 'Postback';     form1.Controls.Add(postback); }  protected void Page_Load(object sender, EventArgs e) {     if (ViewState['controls'] != null)     {         List<string> ids = (List<string>)ViewState['controls'];          for (int i = 0; i < ids.Count; i++)         {             TextBox textbox = new TextBox();             textbox.ID = string.Format('txt_{0}', ids[i]);             textbox.Text = textbox.ID;             controls.Controls.Add(textbox);              Button remove = new Button();             remove.Text = 'Remove';             remove.Click += new EventHandler(removeOnClick);             remove.ID = ids[i];             controls.Controls.Add(remove);              controls.Controls.Add(new LiteralControl('<br />'));         }     } }  protected void removeOnClick(object sender, EventArgs e) {     Control btn = sender as Control;      List<string> ids = (List<string>)ViewState['controls'];     ids.Remove(btn.ID);      //A hacky way to remove the components around the button and the button itself     btn.Parent.Controls.Remove(btn.Parent.Controls[btn.Parent.Controls.IndexOf(btn) + 1]);     btn.Parent.Controls.Remove(btn.Parent.Controls[btn.Parent.Controls.IndexOf(btn) - 1]);     btn.Parent.Controls.Remove(btn);      ViewState['controls'] = ids; }  protected void buttonOnClick(object sender, EventArgs e) {     List<string> ids;      if (ViewState['controls'] == null)         ids = new List<string>();     else         ids = (List<string>)ViewState['controls'];      string id = Guid.NewGuid().ToString();     TextBox textbox = new TextBox();     textbox.ID = string.Format('txt_{0}', id);     textbox.Text = textbox.ID;     controls.Controls.Add(textbox);      Button remove = new Button();     remove.Text = 'Remove';     remove.Click += new EventHandler(removeOnClick);     remove.ID = id;     controls.Controls.Add(remove);      controls.Controls.Add(new LiteralControl('<br />'));      ids.Add(id);      ViewState['controls'] = ids; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll have an ASP.net page that creates some Excel Sheets and sends them to
My asp.net page dynamically displays 207 questions (I can't control this). Each question have
I have a ASP.NET page with an asp:button that is not visible. I can't
I have a asp.net page, and would like to know whether script1 is already
I have an ASP.Net page that will be hosted on a couple different servers,
I have an ASP.NET page which has a script manager on it. <form id=form1
During an ASP.NET page load I'm opening and closing multiple System.Data.SqlClient.SqlConnections inside multiple controls
I have a ASP.Net page using ADO to query MS access database and as
I have an ASP.NET page which has a button it it. The button click
When I serve an ASP.NET page, can I render the various controls on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.