YC Gan
YC Gan

Reputation: 3

C# what the methods do

 protected void Page_Load(object sender, EventArgs e)
 {       
    Panel1.Visible = True;
    Panel2.Visible = false;
    LoadQuestion(); // auto choose question from database and add into Panel1
    LoadQuestion1();  // auto choose question from database and add into Panel2                         
  }

When I start my program, my form automatically loads questions into my textbox and radio button list. I click link button2 to make my Panel1 visible = false and Panel2 visible = true to continue answering question. But after I clicked the link button 2 or 1, it will go back to the Page_Load() method and causes my questions keep on changing.

Upvotes: 1

Views: 100

Answers (3)

PJM
PJM

Reputation: 550

Try:

 protected void Page_Load(object sender, EventArgs e)
 {       
    if (!IsPostBack)
    {
        Panel1.Visible = True;
        Panel2.Visible = false;
        LoadQuestion(); // auto choose question from database and add into Panel1
        LoadQuestion1();  // auto choose question from database and add into Panel2   
    }
 }

Upvotes: 1

Peter Rasmussen
Peter Rasmussen

Reputation: 16922

You should check if it's a postback. You want to execute this only on first load.

protected void Page_Load(object sender, EventArgs e)
{       
   if(!IsPostBack) {
      Panel1.Visible = True;
      Panel2.Visible = false;
      LoadQuestion(); // auto choose question from database and add into Panel1
      LoadQuestion1();  // auto choose question from database and add into Panel2 
   }                      
}

Source

Upvotes: 2

JustAndrei
JustAndrei

Reputation: 859

This is because the Load event happens every time the server processes a request to your page.

There are two kinds of request: initial page load (when you go to the URL) and postback (when you click a button). What you do in your Page_Load method is kinda initialization, so it should be done only initially, but not during the postback.

protected void Page_Load(object sender, EventArgs e)
{       
    if( !IsPostBack )
    {
        // The code here is called only once - during the initial load of the page
    }

    // While the code here is called every time the server processes a request
}

Upvotes: 1

Related Questions