C# textboxes lose value on postback in asp.net

C# textboxes lose value on postback in asp.net

  1. The  textboxes won't save their state after postback. ASP.NET does not handle that for you.
  2. If you put code in your Page_Load event to set the values of the ASP.NET textboxes, the values that were posted back will not be saved, because Page_Load happens after the child control states are restored in the ASP.NET page lifecycle. The values are already restored by ASP.NET, but you are overwriting their restored values.
  3. The correct thing to do to fix #2 is to check Page.IsPostBack before loading your initial state, like this:
    if ( !Page.IsPostBack )
    {
        // set the textbox initial states from the database
    }

Reactions

Posting Komentar

0 Komentar