[英]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. 我单击链接按钮2使我的Panel1
visible = false
,Panel2 visible = true
继续回答问题。 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. 但是在我点击链接按钮2或1后,它将返回到
Page_Load()
方法并导致我的问题不断变化。
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
}
}
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
}
}
This is because the Load event happens every time the server processes a request to your page. 这是因为每次服务器处理对页面的请求时都会发生Load事件。
There are two kinds of request: initial page load (when you go to the URL) and postback (when you click a button). 有两种请求:初始页面加载(当您转到URL时)和回发(当您单击按钮时)。 What you do in your Page_Load method is kinda initialization, so it should be done only initially, but not during the postback.
你在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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.