asp c#

时间:2016-02-24 12:44:04

标签: c# asp.net web

我要求创建一个应从数据库加载的调查Web表单。

每个问题将有1-10个评级项目,并且结果应该保存到带有问题编号的数据库中。

我尝试使用顶部带有标签控件的Web表单和选项的RadioButtonList来实现它,但是一次只能加载一个问题和答案进行显示。我是网络编程的新手。如果有任何工作代码示例或任何想法如何实现这一点,它将是有帮助的。

我已完成编码,将每个问题放在页面上button click我正在加载下一个问题,但我需要在一个页面上提出所有问题。

                public partial class _Default : System.Web.UI.Page 
                {
                public static SqlConnection sqlconn;
                protected string PostBackStr;

                protected void Page_Load(object sender, EventArgs e)
                {
                sqlconn = new SqlConnection(ConfigurationManager.AppSettings["sqlconnstr"].ToString());
                PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "time");
                if (IsPostBack)
                {
                string eventArg = Request["__EVENTARGUMENT"];
                if (eventArg == "time")
                {
                string str = "select * from tbl_Question";
                SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Question");
                int count = ds2.Tables[0].Rows.Count;
                getNextQuestion(count);
                }
                }
                }
                protected void Button1_Click(object sender, EventArgs e)
                {
                Label1.Visible = false;
                txtName.Visible =false;
                Button1.Visible = false;
                Panel1.Visible = true;
                lblName.Text = "Name : " + txtName.Text;
                int score = Convert.ToInt32(txtScore.Text);
                lblScore.Text = "Score : " + Convert.ToString(score);
                Session["counter"]="1";
                Random rnd = new Random();
                int i = rnd.Next(1, 10);//Here specify your starting slno of question table and ending no.
                //lblQuestion.Text = i.ToString();
                getQuestion(i);

                }
                protected void Button2_Click(object sender, EventArgs e)
                {
                string str = "select * from tbl_Question ";
                SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Question");
                int count = ds2.Tables[0].Rows.Count;
                getNextQuestion(count);

                }
                public void getQuestion(int no)
                {
                string str = "select * from tbl_Question where slNo=" + no + "";
                SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Question");
                // int count = ds2.Tables[0].Rows.Count;
                if (ds2.Tables[0].Rows.Count > 0)
                {

                DataRow dtr;
                int i = 0;
                while (i < ds2.Tables[0].Rows.Count)
                {
                dtr = ds2.Tables[0].Rows[i];
                Session["Answer"] = Convert.ToString(Convert.ToInt32 (dtr["Correct"].ToString())-1);
                lblQuestion.Text = "Q." + Session["counter"].ToString() + "  " + dtr["Question"].ToString();
                lblQuestion2.Text = "Q." + Session["counter"].ToString() + "  " + dtr["arQuestion"].ToString();
                LblQNNo.Text = Session["counter"].ToString();
                RblOption.ClearSelection();
                RblOption.Items.Clear();
                RblOption.Items.Add(dtr["Option1"].ToString());
                RblOption.Items.Add(dtr["Option2"].ToString());
                RblOption.Items.Add(dtr["Option3"].ToString());
                RblOption.Items.Add(dtr["Option4"].ToString());
                RblOption.Items.Add(dtr["Option5"].ToString());
                RblOption.Items.Add(dtr["Option6"].ToString());
                RblOption.Items.Add(dtr["Option7"].ToString());
                RblOption.Items.Add(dtr["Option8"].ToString());
                RblOption.Items.Add(dtr["Option9"].ToString());
                RblOption.Items.Add(dtr["Option10"].ToString());

                i++;
                }
                }
                }
                public void getNextQuestion(int cnt)
                {

                if (Convert.ToInt32(Session["counter"].ToString()) < cnt)
                {
                Random rnd = new Random();
                int i = rnd.Next(1, 10);
                lblQuestion.Text = i.ToString();
                getQuestion(i);
                //qst_no = i;
                Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);

                }
                else
                {
                Panel2.Visible = false;
                //code for displaying after completting the exam, if you want to show the result then you can code here.
                }
                }




                protected void Button3_Click(object sender, EventArgs e)
                {
                insertAns();
                }

                private void insertAns()
                {
                SqlCommand cmd;
                sqlconn = new SqlConnection(ConfigurationManager.AppSettings["sqlconnstr"].ToString());
                cmd = new SqlCommand("insert into Tbl_Ans(UserID, Question_ID, Answer) values(@ans, @ans1, @ans2)", sqlconn);

                cmd.Parameters.AddWithValue("@ans", txtName.Text);
                cmd.Parameters.AddWithValue("@ans1", Session["counter"].ToString());
                cmd.Parameters.AddWithValue("@ans2", RblOption.SelectedItem.Text);
                sqlconn.Open();
                int i = cmd.ExecuteNonQuery();
                sqlconn.Close();

                if (i != 0)
                {
                lbmsg.Text = "Your Answer Submitted Succesfully";
                lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
                }
                else
                {
                lbmsg.Text = "Some Problem Occured";
                lbmsg.ForeColor = System.Drawing.Color.Red;
                }
                }
                #region Connection Open
                public void ConnectionOpen()
                {
                try
                {
                if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); }
                }
                catch (SqlException ex)
                {
                lbmsg.Text = ex.Message;
                }
                catch (SystemException sex)
                {
                lbmsg.Text = sex.Message;
                }
                }
                #endregion
                #region Connection Close
                public void ConnectionClose()
                {
                try
                {
                if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); }
                }
                catch (SqlException ex)
                {
                lbmsg.Text = ex.Message;
                }
                catch (SystemException exs)
                {
                lbmsg.Text = exs.Message;
                }
                }
                #endregion

                }

1 个答案:

答案 0 :(得分:1)

首先我想说你应该使用问题表ID代替问题编号来保存答案以备将来使用。

我不太了解dotnet所以我没有在这里附加任何代码。但我可以建议你

  1. 首先将所有带有各自ID的问题提取到一个对象或数组中,或者从它们中获取适配器等。

  2. 然后您可以使用表单使用foreach循环显示它们。例如。

  3. 假设&#34;问题&#34;是一个数组,包含从数据库中提取的所有问题。然后申请

    <form action="abc" method="post">
    
    foreach(questions as question){
             <tr>
                   <td>(print your question here)</td>
                   <td><input type="anything you want" name="(print here    question.id)" />
             </tr>
    }
    
    <input type="submit" value="submit" />
    </form>
    

    现在,您将在表单提交中获取数据,然后您可以使用已经是问题ID的名称轻松访问答案。所以现在两者都相互联系。

    如果不清楚,

    欢迎任何查询。