为sql server表中的列动态生成单选按钮 [英] Generating radio buttons dynamically for columns in a sql server table

查看:71
本文介绍了为sql server表中的列动态生成单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个在线目标tpe问题纸生成器,根据从特定主题生成的问题数量生成随机问题说科学。



i有一个存储过程来随机生成问题并将其插入到临时表中并在网格视图中显示它们。



我还想检索以下问题通过一个单选按钮对应存储在四列中的每个选项,以便用户可以在这些选项中选择一个选项。我该怎么做?我想使用选项选择进一步评估答案。希望我已经清楚了。

任何指导都会有很大帮助.Thanx



(例如)

1.一个字节等于 - 表中的-Col1

optA 2位----表格中的Col2

optB 4位---- Col3

optC 8位 - --- Col4

OptD 16位 - Col5



答案16位 - Col6






我在sql server 2000中的存储过程如下:

I want to create an online objective tpe question paper generator which generates random questions based on the number of questions to be generated from a particular subject say science.

i have a stored procedure to generate the questions randomly and inserting it into a temp table and displaying them in a gridview.

further to this i would like to retrieve the question followed by a radio button against each option stored in four columns so that user can select one option among these.How do i do this?I would like to use the option select to further evaluate the answer.Hope i have made myself clear.
Any guidance will be of great help.Thanx

(eg)
1. One byte equals to ---Col1 in table
optA 2 bits----Col2 in table
optB 4 bits----Col3
optC 8 bits----Col4
OptD 16 bits--Col5

Answer 16 bits--Col6



my stored procedure in sql server 2000 is as below

ALTER PROCEDURE [dbo].[quesRand] (@NO_ques int)
	-- Add the parameters for the stored procedure here
AS
BEGIN
set NOCOUNT ON	
DECLARE @intFlag INT
DECLARE @random INT
DECLARE @upper INT
DECLARE @lower INT

SET @intFlag = 1


set @lower=1
set @upper=100
create table #test4 (cd_no int,cd_name nvarchar(50),date_received smalldatetime,
shipped_with_remarks nvarchar(50))
WHILE (@intFlag <=@NO_ques)


BEGIN
select @random=Round(((@upper-@lower-1))*RAND()+@lower,0)


PRINT @random
insert into #test4
select * from science where cd_no=@random

SET @intFlag = @intFlag + 1

END
---- select * from #test4

END 





i使用Visual Studio 2005和.aspx页面如下







i am using Visual studio 2005 and .aspx page is as below


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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Software CD search</title>
    <script type="text/javascript"  src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function () {
$('#txtNo_ques').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
e.preventDefault();
}
}
});
});
</script>
  
</head>
<body >
    <form id="form1" action="Ques.aspx.cs" runat="server">
    
    
    
    <div align=center>
        <asp:Label ID="lblNo_ques" runat="server" Text="Enter the no. of questions"></asp:Label>
        <asp:TextBox ID="txtNo_ques" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="Validate_No_ques" runat="server" ControlToValidate="txtNo_ques"

            ErrorMessage="Please Enter the no. of questions!"></asp:RequiredFieldValidator>
    </div>
    
    
    <div align="center" style="margin:20px;" >
        <asp:Button ID="genQues" runat="server" Text="Generate & View" OnClick="genQues_Click" />
    </div>
    
    
    <div>
         </div>
    <hr />
    <div align="center" >
        <asp:GridView ID="dgv1" runat="server" BackColor="LightSteelBlue" BorderColor="Crimson" BorderStyle="Double" BorderWidth="2px" ForeColor="Black" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True">
            <EditRowStyle BackColor="Teal" BorderColor="#C00000" BorderWidth="3px" />
            <AlternatingRowStyle BackColor="InactiveCaption" BorderColor="Green" />
            <RowStyle BackColor="#FFE0C0" BorderColor="#004040" BorderStyle="Ridge" BorderWidth="3px" />
            <HeaderStyle BackColor="Thistle" BorderColor="PaleGreen" BorderStyle="Dashed" />
        </asp:GridView>
    
    </div>
    
        
    
    </form>
</body>
</html>







文件Ques.aspx.cs后面的代码如下






My code behind file Ques.aspx.cs is as follows

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;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{

   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
       
        protected void genQues_Click(object sender, EventArgs e)
    {
        string constring = ConfigurationManager.AppSettings.Get("con").ToString();
        SqlConnection conn = new SqlConnection(constring);
        conn.Open();

        SqlCommand check = new SqlCommand("quesRand", conn);
        check.CommandType = CommandType.StoredProcedure;

        check.Parameters.Add(new SqlParameter("@NO_ques", SqlDbType.Int));
        check.Parameters["@NO_ques"].Value = txtNo_ques.Text;
        


        SqlDataAdapter da = new SqlDataAdapter(check);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.dgv1.DataSource = ds.Tables[0].DefaultView;

        dgv1.DataBind();

        conn.Close();
        
    }





代码块已修复[/ edit]



[edit]Code block fixed[/edit]

推荐答案

function (){


' #txtNo_ques')。keydown(函数(e){
if (e.shiftKey || e.ctrlKey || e.altKey){
e。 preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8 )||(key == 46 )||(key> = 35 && key< = 40 )||(key> = 48 && key< = 57 )||(键> = 96 && amp; key< = 105 ))){
e.preventDefault();
}
}
});
});
< / script >

< / head >
< 正文 >
< 表格 id = form1 action = Ques.aspx.cs runat = server >



< div align = center >
< asp:标签 ID = lblNo_ques runat = 服务器 文本 = 输入no。问题 > < / asp:标签 >
< asp:TextBox ID = txtNo_ques runat = 服务器 > < / asp:TextBox >
< asp:RequiredFieldValidator < span class =code-attribute> ID = Validate_No_ques runat = 服务器 ControlToValidate = txtNo_ques

ErrorMessage = 请输入否。问题! > < / asp:RequiredFieldValidator >
< / div >


< div align = center style = margin:20px; >
< asp:Button ID = genQues runat = server 文字 = Generate&查看 OnClick = genQues_Click / >
< / div >


< div >
< / div >
< hr / > ;
< div align = 中心 >
< asp:GridView ID = dgv1 runat = server BackColor = LightSteelBlue BorderColor = Crimson BorderStyle = Double BorderWidth = 2px ForeColor =Black\" AutoGenerateDeleteButton=\"True\" AutoGenerateEditButton=\"True\" AutoGenerateSelectButton=\"True\">
<EditRowStyle BackColor=\"Teal\" BorderColor=\"#C00000\" BorderWidth=\"3px\" />
<AlternatingRowStyle BackColor=\"InactiveCaption\" BorderColor=\"Green\" />
<RowStyle BackColor=\"#FFE0C0\" BorderColor=\"#004040\" BorderStyle=\"Ridge\" BorderWidth=\"3px\" />
<HeaderStyle BackColor=\"Thistle\" BorderColor=\"PaleGreen\" BorderStyle=\"Dashed\" />
</asp:GridView>

</div>



</form>
</body>
</html>
('#txtNo_ques').keydown(function (e) { if (e.shiftKey || e.ctrlKey || e.altKey) { e.preventDefault(); } else { var key = e.keyCode; if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) { e.preventDefault(); } } }); }); </script> </head> <body > <form id="form1" action="Ques.aspx.cs" runat="server"> <div align=center> <asp:Label ID="lblNo_ques" runat="server" Text="Enter the no. of questions"></asp:Label> <asp:TextBox ID="txtNo_ques" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="Validate_No_ques" runat="server" ControlToValidate="txtNo_ques" ErrorMessage="Please Enter the no. of questions!"></asp:RequiredFieldValidator> </div> <div align="center" style="margin:20px;" > <asp:Button ID="genQues" runat="server" Text="Generate & View" OnClick="genQues_Click" /> </div> <div>  </div> <hr /> <div align="center" > <asp:GridView ID="dgv1" runat="server" BackColor="LightSteelBlue" BorderColor="Crimson" BorderStyle="Double" BorderWidth="2px" ForeColor="Black" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True"> <EditRowStyle BackColor="Teal" BorderColor="#C00000" BorderWidth="3px" /> <AlternatingRowStyle BackColor="InactiveCaption" BorderColor="Green" /> <RowStyle BackColor="#FFE0C0" BorderColor="#004040" BorderStyle="Ridge" BorderWidth="3px" /> <HeaderStyle BackColor="Thistle" BorderColor="PaleGreen" BorderStyle="Dashed" /> </asp:GridView> </div> </form> </body> </html>







My code behind file Ques.aspx.cs is as follows






My code behind file Ques.aspx.cs is as follows

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;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{

   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
       
        protected void genQues_Click(object sender, EventArgs e)
    {
        string constring = ConfigurationManager.AppSettings.Get("con").ToString();
        SqlConnection conn = new SqlConnection(constring);
        conn.Open();

        SqlCommand check = new SqlCommand("quesRand", conn);
        check.CommandType = CommandType.StoredProcedure;

        check.Parameters.Add(new SqlParameter("@NO_ques", SqlDbType.Int));
        check.Parameters["@NO_ques"].Value = txtNo_ques.Text;
        


        SqlDataAdapter da = new SqlDataAdapter(check);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.dgv1.DataSource = ds.Tables[0].DefaultView;

        dgv1.DataBind();

        conn.Close();
        
    }





[edit]Code block fixed[/edit]


How about this..?

SQL

1. Table for Questions

2. Table for Multiple Choices

- Having a column for answers true or false will suffice

- This table will have QuestionsID as foreign key

3. Table for User Answers

- Containing UserID(person taking test), QuestionID, UserChoiceID (MultipleChoicesID))

- You will be able to mark as the QuestionID is linked with Multiple Choice, if it’s true is correct.



Application

Load questions one by one on your datagrid(Next button to load next question, do your validations if needed), group your radiobutton and set your command argument to MultipleChoiceID, you will get your QuestionID when loading the question. All this save to 3 above.
How about this..?
SQL
1. Table for Questions
2. Table for Multiple Choices
- Having a column for answers true or false will suffice
- This table will have QuestionsID as foreign key
3. Table for User Answers
- Containing UserID(person taking test), QuestionID, UserChoiceID (MultipleChoicesID))
- You will be able to mark as the QuestionID is linked with Multiple Choice, if it's true is correct.

Application
Load questions one by one on your datagrid(Next button to load next question, do your validations if needed), group your radiobutton and set your command argument to MultipleChoiceID, you will get your QuestionID when loading the question. All this save to 3 above.


这篇关于为sql server表中的列动态生成单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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