我从互联网上得到代码,它包含一个名为 TblQuestions 的数据库表。创建了一个 Default.aspx 页面,其文本框名称为 TxtRequiredRecords & button1。当值 2 在文本框中输入并单击按钮 1 时,它会随机生成 2 个问题。如果输入 5,它会从表中随机生成 5 个问题。我想添加另一个文本框 2 用于输入选项。我的目标是,如果我在文本框 1 和 C 中输入 2在 textbox2.it 中,它将仅从选项 C 问题中随机生成 2 个问题。
我在哪里修改我的代码以获得结果?
我的代码 Default.aspx :-
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TxtRequiredRecords" runat="server"></asp:TextBox>
<asp:Button ID="BtnDisplayRecords" runat="server" OnClick="Button1_Click" Text="Display Records" />
<br />
<br />
<asp:GridView ID="GvResults" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
c#代码:-
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;
public partial class _Default : System.Web.UI.Page
{
static int TotalRecords;
SqlConnection con;
SqlDataAdapter sqlda;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string query = "SELECT COUNT(*) AS TotalRecords FROM TblQuestions";
DataTable dt = GetRecords(query);
TotalRecords = Convert.ToInt32(dt.Rows[0]["TotalRecords"]);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
bool IsInt;
int RequiredRecords;
string CSVData, query;
IsInt = Int32.TryParse(TxtRequiredRecords.Text, out RequiredRecords);
if (IsInt)
{
if (TotalRecords >= RequiredRecords)
{
CSVData = GetRandomNumbersCSV(TotalRecords, RequiredRecords);
query = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY QuestionID) AS RowID,* FROM TblQuestions)TempTable WHERE RowID IN(" + CSVData + ")";
dt = GetRecords(query);
GvResults.DataSource = dt;
GvResults.DataBind();
}
else
{
Response.Write("Required Records must be greater than Requried Records.");
}
}
else
{
Response.Write("Invalid Number");
}
}
public ArrayList RandomNumbers(int max)
{
ArrayList lstNumbers = new ArrayList();
Random rndNumber = new Random();
int number = rndNumber.Next(1, max + 1);
lstNumbers.Add(number);
int count = 0;
do
{
number = rndNumber.Next(1, max + 1);
if (!lstNumbers.Contains(number))
{
lstNumbers.Add(number);
}
count++;
}
while (count <= 10 * max);
return lstNumbers;
}
public string GetRandomNumbersCSV(int max, int req)
{
string CSV = "";
ArrayList lstNumbers = RandomNumbers(max);
for (int i = 0; i < req; i++)
CSV += lstNumbers[i].ToString() + ",";
CSV = CSV.Remove(CSV.Length - 1);
return CSV;
}
public DataTable GetRecords(string Query)
{
con = GetConnection();
con.Open();
sqlda = new SqlDataAdapter(Query, con);
dt = new DataTable();
sqlda.Fill(dt);
con.Close();
return dt;
}
public SqlConnection GetConnection()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
return con;
}
}