What are the main differences in defining a control or changing it dynamically when the page loads?

I am working on a project that dynamically creates the controls for the form in the page_load event, loads their current values ​​from the database and saves their values ​​(using FindControl) when the user clicks the continue button.

When I added the control statically on the .aspx page and followed the same procedure for loading a value in the page loading and saved it when I clicked the button, I found that the value would not be saved correctly. This does not seem to save, because the click event fires after page_load, so the page_load of the message returned the value back, and the value entered by the user was not saved.

The strange thing is that by changing the control that will be dynamically created in the same way as all other controls on the page, while maintaining the loading and saving of the same, it now works. Although loading the page still creates a control with the old database value.

This seems to be a very fundamental function of asp.net, but I just don't understand what is going on. I suspect this is due to the timing of the creation and possibly when the state of the view comes in.

+3
source share
4 answers

Make sure that:

  • Page_Load. , , , ViewState .
  • . , , . ViewState, / , , , .
  • _Load .
+1

, . Page_Load. , , , . , asp.net /.

, , , asp.net , ( ). , .

Asp.Net , , , .

, , ( , ), EnableViewState true .

, Page_Load, , . . Page_Load, .

+2

( ), - ASP.NET.

Microsoft , , .

, / ..

, .

+1

, Page.IsPostBack, .

private void Page_Load()
{
    if (!this.IsPostBack)
    {
        // Assign values to the controls.
    }
}
+1
source

Source: https://habr.com/ru/post/1764438/


All Articles