question

CoreyFleig-6304 avatar image
0 Votes"
CoreyFleig-6304 asked CoreyFleig-6304 commented

How does Postback work with Dynamc Textboxes?

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!

dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered CoreyFleig-6304 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Bruce-SqlWork - That's exactly what I was wondering. Thank you very much.
If I knew what you know, I'm sure I'd turn off viewstate as well.

If you ever offer classes or lessons online, let me know!

0 Votes 0 ·
YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered CoreyFleig-6304 commented

Hi @CoreyFleig-6304 ,
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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@YijingSun-MSFT - Thank you for responding. I seem to have trouble understanding all
the things that go on under the hood. This is one of those things!

Bruce-SqlWork explained how viewstate works, and that's what I was wondering about.

Thank you for your expertise!

0 Votes 0 ·