数据库结构:

题目表Question

QUESTIONBANK(题库)
是否主键 字段名 字段描述 数据类型 长度 可空 约束 缺省值 备注
QUESTIONID 题目编号 INT          
  COURSEID 课程编号 INT        
  CHAPTERSID 章节编号 INT        
  QUESTIONTYPEID 问题类型编号 INT        
  QUESTIONTITLE 问题题目 NVARCHAR(2000) 2000      
  ANSWER 正确答案 NVARCHAR(3000) 3000      
  QUESTIONREMARK 问题备注 NVARCHAR(3000) 3000      
View Code
<form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" OnItemDataBound="Item_Bound" OnItemCommand="DataList1_ItemCommand">
            <ItemTemplate>
                <table style="width: 100%;">
                    <tr>
                        <td>
                            <%#Eval("QuestionID") %>
                        </td>
                        <td>
                            <%#Eval("QUESTIONTITLE")%>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                            </asp:RadioButtonList>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="Button1" CommandName="UpdateIt" runat="server" Text="提交" />
            </FooterTemplate>
        </asp:DataList>
    </div>
    </form>

 

选项表OPTIONS

OPTIONSID 选项编号 INT          
  ANSWERS_N 选项 VARCHAR(2) 2      
  ANSWER_CONTENT 选项内容 VARCHAR(2000) 2000      
  ISTRUEANSWER 正确答案 BIT        
  QUESTIONID 题目编号 INT        

 

default.aspx主要代码:

 

    protected void Page_Load(object sender, EventArgs e)
    {
  
            bindData();
       
    }

    /// <summary>
    /// 绑定题目数据
    /// </summary>
    void bindData()
    {

        //string sql = "select * from QUESTIONBANK";
        //DataSet ds = db.ds(sql, "table1");
        //DataTable dt = ds.Tables["table1"];

        yw.OnStudySystem.Manager.QuestionBankManager question = new yw.OnStudySystem.Manager.QuestionBankManager(this.DbHelper,this.UserInfo);
        yw.OnStudySystem.Model.QuestionBankEntity model = new yw.OnStudySystem.Model.QuestionBankEntity();
        DataTable dt = question.GetDT();
        DataList1.DataSource = dt;
        DataList1.DataKeyField = "QUESTIONID";
        DataList1.DataBind();
    }
    /// <summary>
    /// 绑定选项
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Item_Bound(Object sender, DataListItemEventArgs e)
    {
        
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("RadioButtonList1");

            yw.OnStudySystem.Manager.OptionsManager question = new yw.OnStudySystem.Manager.OptionsManager(this.DbHelper,this.UserInfo);
            yw.OnStudySystem.Model.OptionsEntity model = new yw.OnStudySystem.Model.OptionsEntity();

            string sql = "select * from OPTIONS where QUESTIONID='" + DataList1.DataKeys[e.Item.ItemIndex] + "'";
            // Response.Write(sql);
            DataSet ds = db.ds(sql, "table2");
            DataTable dt = ds.Tables["table2"];
            rbl.DataSource = dt;
            rbl.DataTextField = "ANSWER_CONTENT";
            rbl.DataValueField = "ANSWERS_N";
            rbl.DataBind();

        }
    }
    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DataList1_ItemCommand(Object sender, CommandEventArgs e)
    {
        if (e.CommandName == "UpdateIt")
        {
            foreach (DataListItem oDataListItem in DataList1.Items)
            {
                RadioButtonList rbtl = (RadioButtonList)oDataListItem.FindControl("RadioButtonList1");
                //选项的值
                string str = rbtl.SelectedValue.ToString();
                //题目索引
                string qid = DataList1.DataKeys[oDataListItem.ItemIndex].ToString();


            }
        }
    }

 

 

这个事用来做单选题的题目和选项绑定的写法,也可以参考用来写其他的东西!

@_@.....

好久没有写博文了~~~~大大们支持一下可以不~~~~~

posted on 2012-06-03 09:00  魔_君  阅读(1660)  评论(1编辑  收藏  举报