0
votes

So I have some information I show a user about their account. However, an administrator can see this page, and decide to click "Activate" or "Deactivate" on a user.

Expected behavior: If the administrator presses Activate on an inactive account, the information queries the database and displays at the top that the user's account is active. And the success message is displayed.

The current behavior: If the administrator presses Activate on an inactive account, the information doesn't requery, it still says the user's account is INACTIVE. And the success message is displayed.

I'm using OnClick and runat="server" on the buttons. And the information table that queries the database is in Page_Load(). I'm not using the "IsPostBack" or anything, so it should refresh the information.

Do I have to put a "Reload()" or "Refresh()" or "DataBind()" type function at the end of my "btnUpdate_OnClick(object sender, EventArgs e)" function? How would I do this?

I just don't understand why the event lifecycle won't just reload the page completely.

EDIT:

Some simplified code:

protected void Page_Load(object sender, EventArgs e)
  {

      if (Request["uid"] != null)
          userID = Int32.Parse(Request["uid"]);

      UserDAO ud = new UserDAO();
      tblUser u = ud.GetUser(userID);
      AccountStatus.Text = (((Boolean)u.ActiveOrNot) ? "Active" : "Inactive") + "<br />";

 }

 protected void btnActivate_OnClick(object sender, EventArgs e)
  {
      UserDAO udao = new UserDAO();
      int userBeingEdited = -1;

      if (Request["uid"] != null)
      {
          userBeingEdited = Int32.Parse(Request["uid"]);
      }
      if (udao.ActivateUser(userBeingEdited))
      {
          DisplayError("This user has been activated.", true);
      }
 }

Where AccountStats is a Asp:label. And GetUser queries the database.

That's all the relevant code.

I'm wondering why Onclick doesn't refresh the page and reload Page_Load() function!!

2
I don't understand what you mean. I'm not saving the state or anything.Dexter
What is happening in btnUpdate_Click and what in Page_Load? Do you requery and DataBind after the processing in btnUpdate_Click?rene
look at your question one more time and where you say you are not using isPostBack and where you say run at server.. "RUNAT=SERVER" is the key here..MethodMan
I'm not sure what your asking... the page IS cycled when the postback happens. That's how it all works?asawyer
im prtty sure as @DJ_KRAZE says you need if(!PostBack) when using a runat=serverNot loved

2 Answers

2
votes

OnLoad fires before event handlers like btnActivate_OnClick. Since you are displaying status in OnLoad, the changes made in the button's click event won't be reflected until the next page load.

You can put the code which displays the status into OnPreRender, which fires after event handlers, or you can add an additional method to display status.

private int _uid;

protected void Page_Load( object sender, EventArgs e )
{
    if( Request["uid"] != null )
    {
        _uid = Int32.Parse( Request["uid"] );
    }
    else
    {
        throw new InvalidOperationException( "uid parameter is missing." );
    }

    DisplayStatus();
}

private void DisplayStatus()
{
    UserDAO ud = new UserDAO();
    tblUser u = ud.GetUser( _uid );
    AccountStatus.Text = ( ( (Boolean)u.ActiveOrNot ) ? "Active" : "Inactive" ) + "<br />";
}

protected void btnActivate_OnClick( object sender, EventArgs e )
{
    UserDAO udao = new UserDAO();
    int userBeingEdited = -1;

    if( _uid > default( int ) )
    {
        userBeingEdited = _uid;
    }
    if( udao.ActivateUser( userBeingEdited ) )
    {
        DisplayError( "This user has been activated.", true );
    }

    DisplayStatus();
}
1
votes

It depends on what controls you use to show data on the page. Anyway you may try to call this.DataBind() (or yourLabel.DataBind() etc). Could you show some code?