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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:41:53+00:00 2026-06-13T01:41:53+00:00

Look at this code: static int i = 0; protected void Page_Load(object sender, EventArgs

  • 0

Look at this code:

        static int i = 0;
       protected void Page_Load(object sender, EventArgs e)
    {
        HtmlButton myButton;
        if (!Page.IsPostBack)
        {
            myButton = new HtmlButton();
            myButton.InnerText = "Button first load";
            myButton.ID = i.ToString();
            PlaceHolder1.Controls.Add(myButton);
            i++;
        }
        else
        {
            myButton = new HtmlButton();
            myButton.InnerText = "Button postback" + i.ToString();
            myButton.ID = i.ToString();
            PlaceHolder1.Controls.Add(myButton);
            i++;
        }
    }

expected:

       first load:  "Button first load"
       first postback: first load + "Button postback1"
       second postback: first postback + "Button postback2" ... and so on.

have:

      "Button first load"
      "Button postback1"
      "Button postback2".

Why?

  • 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. Editorial Team
    Editorial Team
    2026-06-13T01:41:54+00:00Added an answer on June 13, 2026 at 1:41 am

    Your expected results are really wrong… 🙂

    Right now what’s happening is exactly what you have written in the code:

    1st load (not postback):

     myButton.InnerText = "Button first load";
    

    Then:

    "Button first load"
    

    2nd, 3rd, nth load (postback):

     myButton.InnerText = "Button postback" + i.ToString();
    

    Then:

    "Button postback1"
    "Button postback2"
    .
    .
    .
    

    UPDATE:

    Now that I understood your problem…

    ASP.NET does not persist state of dynamic controls and thus cannot recreate them after the postback.

    Bottom line: You must recreate your dynamically added controls after each postback.

    Here’s an answer I gave more than 2 years ago that shows you a nice way of handling such situation:

    https://stackoverflow.com/a/2982271/114029

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

Sidebar

Related Questions

First look at this code: class Program { static void Main(string[] args) { var
Take a look at this code: public class Test { public static void main(String...
it doest work:<, here is my code: public void buttonSaveTo_Click(object sender, EventArgs e) {
Look please at this code: public static void main(String[] args) { String[] array =
look at this code : [WebMethod] public static string GetFacilities(string id) { int hotelid
Look at this Java code: class PerformanceTest2{ public static void main(String args[]){ Long sum
Please look at this code: @interface myObject:NSObject -(void)function:(id)param; @end @implementation myObject -(void)function:(id)param { NSLog(@BEFORE);
Have a look at the following code snippet... File1.h void somefunc(int); File1.c #include File1.h
Have a look at this code: #include <iostream> using namespace std; int main() {
I wrote this code for converting binary to text . public static void main(String

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.