why_1001 2015-05-20 11:51 采纳率: 0%
浏览 1547

求助大神关于ASP.NET项目的问题

求助大神,为什么在线考试模块抓取不了数据!图片说明
这段代码有问题吗?

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 RadioButton[] testBtn1 = new RadioButton[10];
private RadioButton[] testBtn2 = new RadioButton[10];
private RadioButton[] testBtn3 = new RadioButton[10];
private RadioButton[] testBtn4 = new RadioButton[10];
private static int[] rightAns = new int[10];
private int score = 0;

public void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("server=WANGSNOW;database=TestOnline;user id=sa;pwd=root");
    con.Open();
    SqlCommand cmd = new SqlCommand("select max(id) from XZ_test", con);
    int maxTestID = (int)cmd.ExecuteScalar();
    int[] testIDs = new int[10];
    Random rand = new Random();
    String testContent = "";
    String ans1 = "";
    String ans2 = "";
    String ans3 = "";
    String ans4 = "";
    String testID = "";
    for (int i = 0; i < 10; i++)
    {
        int j = rand.Next(1, maxTestID);
        int k = 0;
        cmd = new SqlCommand("select id from XZ_test where id=" + j, con);
        while (cmd.ExecuteScalar() == null)
        {
            j = rand.Next(1, maxTestID);
            cmd = new SqlCommand("select id from XZ_test where id=" + j, con);
        }
        while (k <= i)
        {
            cmd = new SqlCommand("select id from XZ_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 XZ_test where id=" + j, con);
        testContent = (String)cmd.ExecuteScalar();
        cmd = new SqlCommand("select testAns1 from XZ_test where id=" + j, con);
        ans1 = (String)cmd.ExecuteScalar();
        cmd = new SqlCommand("select testAns2 from XZ_test where id=" + j, con);
        ans2 = (String)cmd.ExecuteScalar();
        cmd = new SqlCommand("select testAns3 from XZ_test where id=" + j, con);
        ans3 = (String)cmd.ExecuteScalar();
        cmd = new SqlCommand("select testAns4 from XZ_test where id=" + j, con);
        ans4 = (String)cmd.ExecuteScalar();


        l.Text = i + 1 + "." + testContent + "<br>";
        testBtn1[i] = new RadioButton();
        testBtn2[i] = new RadioButton();
        testBtn3[i] = new RadioButton();
        testBtn4[i] = new RadioButton();
        testBtn1[i].GroupName = "g" + i;
        testBtn1[i].Text = "A、" + ans1 + "  ";
        testBtn1[i].ID = "testBtn1" + i;
        testBtn2[i] = new RadioButton();
        testBtn2[i].GroupName = "g" + i;
        testBtn2[i].Text = "B、" + ans2 + "  ";
        testBtn2[i].ID = "testBtn2" + i;
        testBtn3[i] = new RadioButton();
        testBtn3[i].GroupName = "g" + i;
        testBtn3[i].Text = "C、" + ans3 + "  ";
        testBtn3[i].ID = "testBtn3" + i;
        testBtn4[i] = new RadioButton();
        testBtn4[i].GroupName = "g" + i;
        testBtn4[i].Text = "D、" + ans4 + "  ";
        testBtn4[i].ID = "testBtn4" + i;
        Panel1.Controls.Add(l);
        Panel1.Controls.Add(testBtn1[i]);
        Panel1.Controls.Add(testBtn2[i]);
        Panel1.Controls.Add(testBtn3[i]);
        Panel1.Controls.Add(testBtn4[i]);
        l1.Text = "<br><br>";
        Panel1.Controls.Add(l1);
        testIDs[i] = j; 
    }
    if (!IsPostBack)
    {
        for (int i = 0; i < 10; i++)
        {
            cmd = new SqlCommand("select rightAns from XZ_test where id=" + testIDs[i], con);
            rightAns[i] = (int)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 < 10; i++)
    {
        if (testBtn1[i].Checked)
        {
            if (rightAns[i] == 1)
            {
                score += 5;
            }
        }
        else if (testBtn2[i].Checked)
        {
            if (rightAns[i] == 2)
            {
                score += 5;
            }
        }
        else if (testBtn3[i].Checked)
        {
            if (rightAns[i] == 3)
            {
                score += 5;
            }
        }
        else if(testBtn4[i].Checked)
        {
            if (rightAns[i] == 4)
            {
                score += 5;
            }
        }
    }
    Response.Redirect("result.aspx?score=" + score);
}

}

  • 写回答

4条回答 默认 最新

  • 知常曰明 2015-05-20 12:54
    关注
    if (!IsPostBack)
    

    写反了吧。试试

    if (IsPostBack) 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条