How does Postback work with Dynamc Textboxes?

Coreysan 1,606 Reputation points
2021-11-05T00:26:14.67+00:00

I may have a hard time articulating my question, so please forgive me.

In my web form page, I have this:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize_DataTable_and_Grid1();
            }

            Connect_Grid1_to_Boxes();
        }

So on the 1st page load, the initialize routine just creates blank rows/data in a GridView and DataTable.
The next method dynamically creates textboxes and binds the GridView. So when the form is rendered on screen,
it just shows a bunch of empty textboxes. So far, so good.

My question is this:

If I then type some code into one of the textboxes and trigger a AutoPostBack,
the initialize method will get passed over but the Connect_Grid1_to_Boxes() method will run again.

What's interesting to me is that it won't replace or overwrite the code I just manually typed into a textbox.

And I'm okay with that, but I just don't understand why!

I sure hope that makes sense!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,222 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 53,501 Reputation points
    2021-11-05T15:03:17.23+00:00

    When the render is called, a walk of the control tree is done, and the state of each control is saved to the viewstate hidden field. On postback, at Init the viewstate is used to reconstruct the tree. Then when the apply postback data event is called the tree exists.

    If you view source, you will how big the hidden field is. Back when I used webforms, I typically disabled viewstate, and manually rebuilt the tree in init.


1 additional answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,061 Reputation points
    2021-11-05T02:01:30.28+00:00

    Hi @Coreysan ,
    Where do you create the dynamic textbox control? I think the reason for the textbox won't be replaced is you can't find the dynamic textbox control.

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.