这里只论述我写程序的中的出现的问题及解决方式
具体的动态控件的状态理论参阅 思归呓语的文章
程序想实现的功能为:自动生成试题,在添加一个Button事件,进行校对,并且在原先生成的页面上显示正确的答案,今天先写下关于生成时出现的问题
protected
void
Page_Load(
object
sender, EventArgs e)
{

//字符串生成器
SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();
bldr.DataSource = @".SQLExpress";
bldr.InitialCatalog = "examination";
bldr.UserID = "sa";
bldr.Password = "xxxxxxxxx";

//生成SqlConnection对象
SqlConnection cn = new SqlConnection(bldr.ConnectionString);
cn.Open();

string strSQL = " SELECT TOP (10) subjectid, subjectcontent, rightkey FROM judge ORDER BY NEWID()";

//创建SqlCommand对象
SqlCommand cmd_judge = new SqlCommand(strSQL, cn);
SqlDataReader Qreader = cmd_judge.ExecuteReader();

int Qnumber = 1;


//动态创建控件

while (Qreader.Read())
{
Literal QLiteral = new Literal();
QLiteral.Mode = LiteralMode.Transform;
if (Qnumber != 1) QLiteral.Text = QLiteral.Text + "<br><br>";
QLiteral.Text = QLiteral.Text + Qnumber.ToString() + " " + "【" + Qreader["subjectid"].ToString() + "】" + Qreader["subjectcontent"].ToString();
Panel1.Controls.Add(QLiteral);

//加入正确答案的label
Label Alabel = new Label();
Alabel.ID = "Alab" + Qnumber.ToString();
Alabel.Text = Qreader["rightkey"].ToString().Trim();
Alabel.ForeColor = Color.Red;
Alabel.Visible = false;
Panel1.Controls.Add(Alabel);

//加入正确或错误的选择
RadioButtonList Qradio = new RadioButtonList();
Qradio.ID = "Qrad" + Qnumber.ToString();
Qradio.Items.Add("正确");
Qradio.Items[0].Value = "True";
Qradio.Items.Add("错误");
Qradio.Items[1].Value = "False";
Panel1.Controls.Add(Qradio);
Qnumber++;
Response.Write(Qradio.SelectedIndex);

}
}
这是最初的版本,但很快发现的问题是,当初发button事件时,试题又再一次生成,因此产生错误
但又不能单纯简单的将 Page_Load里面的代码放到if (!IsPostBack)中,因为这样,虽然生成一次试卷,但在button时间PostBack后,试卷无法保存
最后参阅思归的文章帖子等,改为如下:
protected
void
Page_Load(
object
sender, EventArgs e)
{

//字符串生成器
SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();
bldr.DataSource = @".SQLExpress";
bldr.InitialCatalog = "examination";
bldr.UserID = "sa";
bldr.Password = "1986324";

//生成SqlConnection对象
SqlConnection cn = new SqlConnection(bldr.ConnectionString);
cn.Open();

string strSQL = " SELECT TOP (10) subjectid, subjectcontent, rightkey FROM judge ORDER BY NEWID()";

//创建SqlCommand对象
SqlCommand cmd_judge = new SqlCommand(strSQL, cn);
SqlDataReader Qreader = cmd_judge.ExecuteReader();

int Qnumber = 1;


//动态创建控件

while (Qreader.Read())
{
Literal QLiteral = new Literal();
QLiteral.Mode = LiteralMode.Transform;
Panel1.Controls.Add(QLiteral);
if (!IsPostBack)
{
if (Qnumber != 1) QLiteral.Text = QLiteral.Text + "<br><br>";
QLiteral.Text = QLiteral.Text + Qnumber.ToString() + " " + "【" + Qreader["subjectid"].ToString() + "】" + Qreader["subjectcontent"].ToString();
}

//加入正确答案的label
Label Alabel = new Label();
Panel1.Controls.Add(Alabel);
if (!IsPostBack)
{
Alabel.ID = "Alab" + Qnumber.ToString();
Alabel.Text = Qreader["rightkey"].ToString().Trim();
Alabel.ForeColor = Color.Red;
Alabel.Visible = false;
}

//加入选项
RadioButtonList Qradio = new RadioButtonList();
Panel1.Controls.Add(Qradio);
if (!IsPostBack)
{
Qradio.ID = "Qrad" + Qnumber.ToString();
Qradio.Items.Add("正确");
Qradio.Items[0].Value = "True";
Qradio.Items.Add("错误");
Qradio.Items[1].Value = "False";
}
Qnumber++;

}
}
再次运行后程序在PostBack后,正确保存
但答案校对出现问题,解决中...
具体的动态控件的状态理论参阅 思归呓语的文章
程序想实现的功能为:自动生成试题,在添加一个Button事件,进行校对,并且在原先生成的页面上显示正确的答案,今天先写下关于生成时出现的问题




























































这是最初的版本,但很快发现的问题是,当初发button事件时,试题又再一次生成,因此产生错误
但又不能单纯简单的将 Page_Load里面的代码放到if (!IsPostBack)中,因为这样,虽然生成一次试卷,但在button时间PostBack后,试卷无法保存
最后参阅思归的文章帖子等,改为如下:








































































再次运行后程序在PostBack后,正确保存
但答案校对出现问题,解决中...