📄 test2.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private TextBox[] TextBox1 = new TextBox[5];
private string testContent = "";
private int score = 0;
private static string[] rightAns = new string[5];
int[] testIDs = new int[5];
public void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=TestOnline;uid=sa;pwd=123;");
con.Open();
SqlCommand cmd = new SqlCommand("select max(id) from TK_test", con);
int maxTestID = (int)cmd.ExecuteScalar();
Random rand = new Random();
for (int i = 0; i < 5; i++)
{
int j = rand.Next(1, maxTestID);
int k = 0;
cmd = new SqlCommand("select id from TK_test where id=" + j, con);
while (cmd.ExecuteScalar() == null)
{
j = rand.Next(1, maxTestID);
cmd = new SqlCommand("select id from TK_test where id=" + j, con);
}
while (k <= i)
{
cmd = new SqlCommand("select id from TK_test where id=" + j, con);
if (j == testIDs[k] || cmd.ExecuteScalar() == null)
{
j = rand.Next(1, maxTestID);
k = 0;
}
else
{
k++;
}
}
Literal l = new Literal();
Literal l1 = new Literal();
cmd = new SqlCommand("select testContent from TK_test where id=" + j, con);
testContent = (String)cmd.ExecuteScalar();
TextBox1[i] = new TextBox();
l.Text = i + 1 + ". " + testContent + "<br>";
Panel1.Controls.Add(l);
Panel1.Controls.Add(TextBox1[i]);
l1.Text = "<br><br>";
Panel1.Controls.Add(l1);
testIDs[i] = j;
}
if (!IsPostBack)
{
for (int i = 0; i < 5; i++)
{
cmd = new SqlCommand("select rightAns from TK_test where id=" + testIDs[i], con);
rightAns[i] = Convert.ToString( cmd.ExecuteScalar() );
}
}
Button b = new Button();
b.Text = "提交";
b.Click += new System.EventHandler(this.b_click);
Literal lc = new Literal();
Literal lc1 = new Literal();
lc.Text = "<center>";
lc1.Text = "</center>";
Panel1.Controls.Add(lc);
Panel1.Controls.Add(b);
Panel1.Controls.Add(lc1);
con.Close();
}
public void b_click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
string a = TextBox1[i].Text.Trim();
string b = rightAns[i].Trim();
if (TextBox1[i].Text.Trim() == rightAns[i].Trim())
{
score += 5;
}
}
Response.Redirect("result2.aspx?score=" + score);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -