Eugenio Escobar
Eugenio Escobar

Reputation: 21

How should i use the "PostBack"?

Clarification: I am chilean, so my english is not perfect, sorry for the misspellings.

Hi, I am working with an image in c#.

I try to put an example image when the page open the first time, I used the post back for this, but when I press a button, execute the code in the post back section (which is right), after that it execute the Button code, but then, it pass again for the Page_Load method, and execute the "not post back" section, and i dont know why.

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        //Is post back
    }
    else // Is not post back
    {
        //Make things only when the page is open for the first time
    }
}

Upvotes: 0

Views: 92

Answers (1)

JM Tolentino
JM Tolentino

Reputation: 11

I usually only use (!IsPostBack) on PageLoad for doing initial data loads or validations(like settings for users).

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
     if (userIsAdmin)
     {
       button1.Enabled = true;
     }
  }
}

You could refer to the link for the explanation for PostBack https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8

Upvotes: 1

Related Questions