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 172393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:09:29+00:00 2026-05-11T13:09:29+00:00

I read about the dynamic control creation in ASP.NET this piece of text: …When

  • 0

I read about the dynamic control creation in ASP.NET this piece of text:

…When using dynamic controls, you must remember that they will exist only until the next postback. ASP.NET will not re-create a dynamically added control. If you need to re-create a control multiple times, you should perform the control creation in the Page.Load event handler. This has the additional benefit of allowing you to use view state with your dynamic control. Even though view state is normally restored before the Page.Load event, if you create a control in the handler for the Page.Load event, ASP.NET will apply any view state information that it has after the Page.Load event handler ends. This process is automatic …

I wanted to try it on example create a button declaratively –

<asp:Button ID='Button1' runat='server' Text='Button'               onclick='Button1_Click'  /> 

and dynamically on behind code 5 checkboxes –

protected void Page_Load(object sender, EventArgs e) {   for (int i = 0; i <= 5; i++)     {       var chBox = new HtmlInputCheckBox();       Controls.Add(chBox);     }  } 

But when i check some checkboxes and hit the button, after postback all checkboxes states are erased. It mean ASP.NET does not manage view states of dynamic controls automatically? I tried to enable view state to each of checkbox and for whole page, but its doesn’t work. Can someone explain: 1. Why is it so? 2. How to avoid this?

  • 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-11T13:09:29+00:00Added an answer on May 11, 2026 at 1:09 pm

    The controls can be created on Page_Init.

    protected void Page_Init(object sender, EventArguments e) {   //Generate the checkboxes dynamically here.   CheckBox c;   for (int i = 0; i < 5; i++) {     c = new CheckBox();     c.ID = 'Checkbox' + i.ToString();      divContainer.Controls.Add(c); //create a div with runat='Server' attribute or use an asp:Panel, etc. container controls.   } } 

    After that, try clicking the button again, the state will be always be maintained.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently read about Dynamic Creation as one of the design pattern in Cocoa.
ih ave read about the technique to allow dynamic HTML stylesheets using PHP's preprocessor,
I recently read a question on here about static and dynamic linking, which reminded
I read about using the CASE expression inside the WHERE clause here: http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/ I'm
I read about using <context:component-scan base-package=tld.mydomain.business> <context:include-filter type=annotation expression=org.springframework.stereotype.Service/> </context:component-scan> and annotate my service
I read some steps about this one but I can't really get it. I'm
Everywhere I read about the new DLR in .net 4, they say that a
I feel bad about not getting this. But, although I have read several articles
I read some stuff about PropertyGrid (cause I am pretty new to that control)
Read about Server push here . I want to push data to client from

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.