How to update page data after event processing?

On Page_Init I create a table of dynamically created controls based on several database tables. One control is an ImageButton to move a list item up the list. This event handler updates the SortOrder column in the database for the affected items.

Now the problem is that since the controls are created in the Page_Init event, and SortOrder is updated later when the ImageButton command event is fired. What is the best procedure for updating a table with the correct SortOrder. If I recreate the table after the event was fired, the ImageButton command event no longer works.

  • Should I implement a method for updating data in a table without re-creating it?
  • Should I reload the page in code after the event has been fired?

What is your preferred way to solve this problem?

+3
source share
3 answers

Page events, such as Initand Load, will always be fired before the event handler, which raised the postback. This is the basis of the page life cycle (for a visual presentation of Peter Bromberg, see here ). Most developers new to ASP.NET have a serious problem understanding and properly handling this "fix".

:

. Page_Init ( BindData() ), . , . IOW, Page_Init , .

: BindData() ImageButton . ImageButton_Click. .

. ImageButton_Click , BindData() , .

, :

  • Page_Init
  • BindData()

( ):

  • Page_Init
  • BindData() - Eventhandler ImageButton.
  • ImageButton_Click
  • BindData()
+8

- ...

  • OnInit (IsPostBack = false)
    • ImageButton
    • Wireup ImageButton
    • . /. ;

  • OnInit (IsPostBack = true/1st Postback)

    • ImageButton
    • Wireup ImageButton
    • -
  • ImageButton_OnClick ( -)

    • -
    • Viewstate/Session

  • OnInit (IsPostBack = true/2nd )
    • ImageButton
    • Wireup ImageButton
    • . /. , .
+4

-, , , . Asp.Net , . , GridView, Html . , .

, , - .

...

  • .

:

private void Page_Load (...)
{
    if (!IsPostBack)
        //On First Load
        BindData(defaultSoortOrder);
    else
        BindData(currentSortOrder);            
}

private void ImageButton_Click (...)
{
    currentSortOrder = newSortOrder;
    BindData(currentSortOrder);
}

"", BindData . , , .

+1

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


All Articles