protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadQuestion(); //randomly choose question from database and put inside panel1
LoadQuestion1();//randomly choose question from database and put inside panel2
Panel1.visible=true;
Panel2.visible=false;
}
void LoadQuestion()
{
while (dtr.Read())
{
question[index, 0] = dtr["Question"].ToString().Trim();
question[index, 1] = dtr["OptionA"].ToString().Trim();
question[index, 2] = dtr["OptionB"].ToString().Trim();
question[index, 3] = dtr["OptionC"].ToString().Trim();
question[index, 4] = dtr["OptionD"].ToString().Trim();
question[index, 5] = dtr["Answer"].ToString().Trim();
question[index, 6] = dtr["QuestionNo"].ToString().Trim();
index++;
}
for(i=0;i<10;i++)
{
Label lbl = new Label();
RadioButtonList rad = new RadioButtonList();
lbl.ID = "lbl" + i.ToString();
rad.ID = "rad" + i.ToString();
lbl.Text = i + ". " + question[num, 0].ToString();
rad.Items.Add(question[i, 1]);
rad.Items.Add(question[i, 2]);
rad.Items.Add(question[i, 3]);
rad.Items.Add(question[i, 4]);
Panel1.Controls.Add(lbl);
Panel1.Controls.Add(rad);
}
}
void LoadQuestion1()
{
for(i=11;i<20;i++)
{
Label lbl = new Label();
RadioButtonList rad = new RadioButtonList();
lbl.ID = "lbl" + i.ToString();
rad.ID = "rad" + i.ToString();
lbl.Text = i + ". " + question[num, 0].ToString();
rad.Items.Add(question[i, 1]);
rad.Items.Add(question[i, 2]);
rad.Items.Add(question[i, 3]);
rad.Items.Add(question[i, 4]);
Panel2.Controls.Add(lbl);
Panel2.Controls.Add(rad);
}
}
protected void lb1_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = false;
}
protected void lb2_Click(object sender, EventArgs e)
{
Panel2.Visible = true;
Panel1.Visible = false;
}
在这种情况下,如果我没有把!IsPostBack
Page_Load 放在里面,一旦我点击lb1
or lb2
,问题就会不断变化.. 放入!IsPostBack
里面之后Page_Load
,一旦我点击lb1
or lb2
,Panel1
然后就Panel2
什么都没有了......我的页面变得完全空白.. . 怎么了???