我有问题...你能帮我吗 [英] i have problem in it... can u help me

查看:80
本文介绍了我有问题...你能帮我吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cs文件

cs file

using System;
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Collections;




public partial class _Default : System.Web.UI.Page 
{
    public static OleDbConnection  conn;
    protected string PostBackStr;
    int startid = 1;//Here specify your starting id of Questions table. So that it will display questions from id starting from this value
    int endid = 5;//Here specify your ending id of Questions table. So that it will display questions which has id below this value
    int totalnoofquestions = 10;//Here change the number of questions you want to display.
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\WebSite1\\App_Data\\Studb.mdb;Persist Security Info=True");
        PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "time");

        if (IsPostBack)
        {
            string eventArg = Request["__EVENTARGUMENT"];
            if (eventArg == "time")
            {
                getNextQuestion();
            }
        }

    }
    protected void Finish_Click(object sender, EventArgs e)
    {
        if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
        {
            int score = Convert.ToInt32(txtScore.Text) + 1;// 1 for mark for each question
            txtScore.Text = score.ToString();
            lblScore.Text = "Score : " + Convert.ToString(score);
        }
        lblResult.Text = "Thank you for test our application. Your Score is : " + txtScore.Text;
        lblResult.Visible = true;
        Panel2.Visible = false;
    }
    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(startid, endid + 1);
        getQuestion(i);
        ArrayList al = new ArrayList();
        al.Add(i.ToString());
        Session["ids"] = al;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        getNextQuestion();
    }
    public void getQuestion(int no)
    {

        string str = "select * from question where id=" + no + "";
        OleDbDataAdapter da2 = new OleDbDataAdapter (str, conn);
        DataSet ds2 = new DataSet();
        da2.Fill(ds2, "question");
        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();
                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());
                i++;
            }
        }
    }
    public void getNextQuestion()
    {
        //Finish.Visible = false;
        if (Convert.ToInt32(Session["counter"].ToString()) < totalnoofquestions)
        {
            if (RblOption.SelectedIndex >= 0)
            {
                if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
                {
                    int score = Convert.ToInt32(txtScore.Text) + 1;// 1 for mark for each question
                    txtScore.Text = score.ToString();
                    lblScore.Text = "Score : " + Convert.ToString(score);
                }
            }
            Random rnd = new Random();
            int i = rnd.Next(startid, endid);
            ArrayList al = (ArrayList)Session["ids"];
            if (!al.Contains(i.ToString()))
            {
                al.Add(i.ToString());
            }
            else
            {
                while (al.Contains(i.ToString()))
                {
                    i = rnd.Next(startid, endid + 1);

                    if (al.Count == totalnoofquestions - 1 && !al.Contains(i.ToString()))
                    {
                        Button2.Visible = false;
                        Finish.Visible = true;
                        break;
                    }
                    else if (al.Count > endid + 1)
                    {
                        break;
                    }

                }
                if (!al.Contains(i.ToString()))
                {
                    al.Add(i.ToString());
                }
            }
            if (al.Count == totalnoofquestions)
            {
                Button2.Visible = false;
                Finish.Visible = true;
            }
            Session["ids"] = al;
            Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);
            getQuestion(i);



        }
        else
        {
            Panel2.Visible = false;
            //code for displaying after completting the exam, if you want to show the result then you can code here.
        }
    }
    public void ConnectionOpen()
    {
        try
        {
            if (conn.State == ConnectionState.Closed) { conn.Open(); }
        }
        catch (OleDbException ex)
        { }
        catch (SystemException sex)
        { }
    }
    public void ConnectionClose()
    {
        try
        {
            if (conn.State != ConnectionState.Closed) { conn.Close(); }
        }
        catch (OleDbException ex)
        { }
        catch (SystemException sex)
        { }
    }
}

aspx文件

aspx file

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" >
    (function () {
  function display( notifier, str ) {
    document.getElementById(notifier).innerHTML = str;
  }

  function toMinuteAndSecond( x ) {
    return Math.floor(x/60) + ":" + (x=x%60 < 10 ? 0 : x);
  }

  function setTimer( remain, actions ) {
    var action;
    (function countdown() {
       display("countdown", toMinuteAndSecond(remain));
       if (action = actions[remain]) {
         action();
       }
       if (remain > 0) {
         remain -= 1;
         setTimeout(arguments.callee, 1000);
       }
    })(); // End countdown
  }

  setTimer(20, {
    10: function () { display("notifier", "Just 10 seconds to go"); },
     5: function () { display("notifier", "5 seconds left");        },
     0: function () { display("notifier", "Time is up baby");       }
  });
})();
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 <asp:Button ID="Button1" runat="server" Style="z-index: 102; left: 321px; position: absolute;
 top: 18px" Text="Start Exam" ToolTip="Enter Your Name" OnClick="Button1_Click" />
 <asp:TextBox ID="txtScore" runat="server" Style="z-index: 103; left: 681px; position: absolute;
 top: 276px" Visible="False" Width="63px">0</asp:TextBox>

 <asp:Panel ID="Panel1" runat="server" BackColor="#E0E0E0" BorderColor="#E0E0E0" Height="264px"

 Style="z-index: 104; left: 60px; position: absolute; top: 54px" Visible="False"

 Width="707px" ForeColor="#0000C0">
 &nbsp;
 <asp:Label ID="lblScore" runat="server" ForeColor="Green" Style="z-index: 102; left: 567px;
 position: absolute; top: 11px" Text="Score : " Width="136px"></asp:Label>
 <asp:Panel ID="Panel3" runat="server" Height="14px" Width="119px" style="left:427px; z-index: 106; position: absolute; top: 10px;">
 <span id="cd" style ="left:100px;"></span>
 </asp:Panel>
 <asp:Panel ID="Panel2" runat="server" Height="214px" Style="z-index: 103; left: 8px;
 position: absolute; top: 41px" Width="696px">
 <asp:Label ID="lblQuestion" runat="server" Style="z-index: 100; left: 3px; position: absolute;
 top: 7px" Text="Label" Width="682px"></asp:Label>
 <asp:RadioButtonList ID="RblOption" runat="server" Style="z-index: 102; left: 30px;
 position: absolute; top: 36px" Width="515px">
 </asp:RadioButtonList>
 <asp:Button ID="Finish" runat="server" Style="z-index: 106; left: 289px; position: absolute;
 top: 178px" Text="Finish" ToolTip="Click Here to Finish The Test" Visible ="false" OnClick="Finish_Click" />

     <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="z-index: 106; left: 381px; position: absolute;
 top: 179px; width: 41px;" Text="Next" ToolTip="Click Here to Take Next Question" />

 </asp:Panel>
 <asp:Label ID="lblResult" Style="z-index: 107; left: 189px; position: absolute;
 top: 128px" runat="server" Visible ="false" Font-Bold ="true" ForeColor ="Green"  Text=""></asp:Label>
 </asp:Panel>

    </div>
    </form>
</body>
</html>

推荐答案



我在asp.net中编写了一些用于生成在线考试的代码.

我的数据库表包含以下列


I wrote some code for generating online exam in asp.net.

my database table contains following columns
 QuestionTab is table name
columns are qid,qname,opt1,opt2,opt3,opt4,ans



之后,我的设计页面如下所示



And after that my design page is like below

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language ="javascript" >
        var tim;
       
        var min = 20;
        var sec = 60;
        var f = new Date();
        function f1() {
            f2();
            document.getElementById("starttime").innerHTML = "Your started your Exam at " + f.getHours() + ":" + f.getMinutes();
             
            document.getElementById("endtime").innerHTML = "Your  time is :"+f.toLocaleTimeString();
        }
        function f2() {
            if (parseInt(sec) > 0) {
                sec = parseInt(sec) - 1;
                document.getElementById("showtime").innerHTML = "Your Left Time  is :"+min+" Minutes ," + sec+" Seconds";
                tim = setTimeout("f2()", 100);
            }
            else {
                if (parseInt(sec) == 0) {
                    min = parseInt(min) - 1;
                    if (parseInt(min) == 0) {
                        clearTimeout(tim);
                        location.href = "default5.aspx";
                    }
                    else {
                        sec = 60;
                        document.getElementById("showtime").innerHTML = "Your Left Time  is :" + min + " Minutes ," + sec + " Seconds";
                        tim = setTimeout("f2()", 100);
                    }
                }
               
            }
        }
    </script>
</head>
<body onload="f1()">
    <form id="form1" runat="server">
    <div>
      <table width="100%" align="center">
        <tr>
          <td colspan="2">
            <h2>This is head part for showing timer and all other details</h2>
          </td>
        </tr>
        <tr>
          <td>
            <div id="starttime"></div><br />
            <div id="endtime"></div><br />
            <div id="showtime"></div>
          </td>
        </tr>
        <tr>
          <td>
              <asp:datalist id="DataList1" runat="server" width="100%" xmlns:asp="#unknown">
                  onitemdatabound="DataList1_ItemDataBound">
                <HeaderTemplate >
                  <table width="100%" align="center">
                                 
                </HeaderTemplate>
                <itemtemplate>
                   <tr>
                     <td>
                       <table width="100%" border="1" bordercolor="green" cellspacing="0" cellpadding="0">
                         <tr>
                           <td>
                               <asp:label id="Label1" runat="server" text="<%#Eval("qid") %>"></asp:label> <%#Eval("qname") %></td>
                         </tr>
                         <tr>
                           <td>
                           <asp:radiobuttonlist id="RadioButtonList1" runat="server">
           
                               </asp:radiobuttonlist>
                                               
                           </td>
                         </tr>
                       </table>
                     </td>
                   </tr>
               </itemtemplate>
                <footertemplate>
                    </footertemplate></table>
                
              </asp:datalist>
                 
          </td>
         
        </tr>
      </table>
    </div>
    </form>
</body>
</html>



And then my code behind file contains following code.



And then my code behind file contains following code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        MylocalDataClassesDataContext db = new MylocalDataClassesDataContext();
        var f = from d in db.QuestionTabs
                select d;
        DataList1.DataSource = f;
        DataList1.DataBind();
    }
}

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        RadioButtonList rdblist = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
        Label lblqid = (Label)e.Item.FindControl("Label1");
        string qid = lblqid.Text;
        MylocalDataClassesDataContext db = new MylocalDataClassesDataContext();
        var f = from d in db.QuestionTabs
                where d.qid ==int.Parse (qid)
                select d;
        foreach (QuestionTab qt in f)
        {
            rdblist.Items.Add(new ListItem(qt.opt1, qt.opt1));
            rdblist.Items.Add(new ListItem(qt.opt2, qt.opt2));
            rdblist.Items.Add(new ListItem(qt.opt3, qt.opt3));
            rdblist.Items.Add(new ListItem(qt.opt4, qt.opt4));
        }
    }

}



Try this code .Here I used linq for retrieving data instead of this you can use normal process for retrieving data from database

最好的



Try this code .Here I used linq for retrieving data instead of this you can use normal process for retrieving data from database

All the Best


这篇关于我有问题...你能帮我吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆