1
 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                         
  }

当我启动我的程序时,我的表单会自动将问题加载到我的文本框和单选按钮列表中。我单击链接 button2 使我的 Panel1visible = false和 Panel2visible = true继续回答问题。但是在我点击链接按钮 2 或 1 后,它会回到Page_Load()方法并导致我的问题不断变化。

4

3 回答 3

2

您应该检查它是否是回发。您只想在第一次加载时执行此操作。

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 
   }                      
}

资源

于 2013-10-22T17:33:19.310 回答
1

这是因为每次服务器处理对您的页面的请求时都会发生 Load 事件。

有两种请求:初始页面加载(当您转到 URL 时)和回发(当您单击按钮时)。您在 Page_Load 方法中所做的是有点初始化,所以它应该只在最初完成,而不是在回发期间完成。

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
}
于 2013-10-22T17:37:35.550 回答
1

尝试:

 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   
    }
 }
于 2013-10-22T17:33:07.983 回答