📄 regroup.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace examonline
{
/// <summary>
/// regroup 的摘要说明。
/// </summary>
public partial class regroup : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if((bool)Session["IsLogined"]==false)
{
Response.Redirect("index.aspx");
}
Label1.Text=(string)Session["info"];
Session["info"]="";
TextBox txtTitle=new TextBox();
TextBox txtTesttime=new TextBox();
TextBox txtHowtime=new TextBox();
TextBox txtTester=new TextBox();
TextBox txtTeacher=new TextBox();
txtTitle.Width=350;
txtTesttime.Width=350;
txtHowtime.Width=350;
txtTester.Width=350;
txtTeacher.Width=350;
tbStep1.Rows[0].Cells[1].Controls.Add(txtTitle);
tbStep1.Rows[1].Cells[1].Controls.Add(txtTesttime);
tbStep1.Rows[2].Cells[1].Controls.Add(txtHowtime);
tbStep1.Rows[3].Cells[1].Controls.Add(txtTester);
tbStep1.Rows[4].Cells[1].Controls.Add(txtTeacher);
if(!IsPostBack)
{
Bindgrid("");
}
}
private void Bindgrid(string key)
{
string sql1= "Select title,question,solution from selectItems where question like '%"+key+"%'";
string sql2= "Select title,question,solution from fillblank where question like '%"+key+"%'";
string sql3= "Select title,question,solution from essay where question like '%"+key+"%'";
SqlConnection cn = new SqlConnection("data source=(local);Integrated Security=true;database=examonline");
SqlDataAdapter da1 =new SqlDataAdapter(sql1, cn);
SqlDataAdapter da2 =new SqlDataAdapter(sql2, cn);
SqlDataAdapter da3 =new SqlDataAdapter(sql3, cn);
DataSet ds=new DataSet();
try
{
cn.Open();
da1.Fill(ds, "selectItems");
da2.Fill(ds, "fillblank");
da3.Fill(ds, "essay");
}
finally
{
cn.Close();
}
if(key!="")
{
DataTable table=ds.Tables["selectItems"];
foreach(DataRow row in table.Rows)
{
row["question"]=row["question"].ToString().Replace(key,"<font style='BACKGROUND-COLOR: #87cefa'>"+key+"</font>");
}
table=ds.Tables["fillblank"];
foreach(DataRow row in table.Rows)
{
row["question"]=row["question"].ToString().Replace(key,"<font style='BACKGROUND-COLOR: #87cefa'>"+key+"</font>");
}
table=ds.Tables["essay"];
foreach(DataRow row in table.Rows)
{
row["question"]=row["question"].ToString().Replace(key,"<font style='BACKGROUND-COLOR: #87cefa'>"+key+"</font>");
}
}
Datagrid1.DataSource = ds.Tables["selectItems"].DefaultView;
Datagrid1.DataBind();
Datagrid2.DataSource = ds.Tables["fillblank"].DefaultView;
Datagrid2.DataBind();
Datagrid3.DataSource = ds.Tables["essay"].DefaultView;
Datagrid3.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
protected void Button1_Click(object sender, System.EventArgs e)
{
TextBox title=(TextBox)tbStep1.Rows[0].Cells[1].Controls[0];
TextBox Testtime=(TextBox)tbStep1.Rows[1].Cells[1].Controls[0];
TextBox Howtime=(TextBox)tbStep1.Rows[2].Cells[1].Controls[0];
TextBox Tester=(TextBox)tbStep1.Rows[3].Cells[1].Controls[0];
TextBox Teacher=(TextBox)tbStep1.Rows[4].Cells[1].Controls[0];
if(title.Text=="")
{
Session["info"]="试卷名不能为空!";
Response.Redirect("regroup.aspx");
Response.End();
}
string temp="";
SqlConnection cn = new SqlConnection("data source=(local);Integrated Security=true;database=examonline");
string sql = "insert into exam (exam_title,create_time,test_time,many_time,tester,teacher) values ('"+HTMLEncode(title.Text)+"','"+DateTime.Now+"','"+Testtime.Text+"','"+HTMLEncode(Howtime.Text)+"','"+HTMLEncode(Tester.Text)+"','"+HTMLEncode(Teacher.Text)+"') SELECT CAST(scope_identity() AS int)";
SqlCommand cmd = new SqlCommand(sql,cn);
try
{
cn.Open();
temp=cmd.ExecuteScalar().ToString();
}
catch(SqlException ex)
{
Session["info"]="数据库错误:" + ex.Message.ToString();
}
finally
{
cn.Close();
}
if(temp=="")
{
Session["info"]="出错了,请确认你输入的数据格式正确!";
Response.Redirect("regroup.aspx");
Response.End();
}
string sql1= "Select title,question,solution from selectItems";
string sql2= "Select title,question,solution from fillblank";
string sql3= "Select title,question,solution from essay";
SqlDataAdapter da1 =new SqlDataAdapter(sql1, cn);
SqlDataAdapter da2 =new SqlDataAdapter(sql2, cn);
SqlDataAdapter da3 =new SqlDataAdapter(sql3, cn);
DataSet ds=new DataSet();
try
{
cn.Open();
da1.Fill(ds, "selectItems");
da2.Fill(ds, "fillblank");
da3.Fill(ds, "essay");
}
finally
{
cn.Close();
}
CheckBox cb=new CheckBox();
// DataTable dt1=new DataTable("dt1");
// dt1.Columns.Add("title");
// dt1.Columns.Add("question");
// dt1.Columns.Add("solution");
// DataTable dt2=new DataTable("dt2");
// dt2.Columns.Add("title");
// dt2.Columns.Add("question");
// dt2.Columns.Add("solution");
// DataTable dt3=new DataTable("dt3");
// dt3.Columns.Add("title");
// dt3.Columns.Add("question");
// dt3.Columns.Add("solution");
DataRow dr;
for(int i=0;i<Datagrid1.Items.Count;i++)
{
cb=(CheckBox)Datagrid1.Items[i].FindControl("CheckBox1");
if(cb.Checked==true)
{
dr=ds.Tables["selectItems"].NewRow();
dr["title"]=Datagrid1.Items[i].Cells[1].Text;
dr["question"]=Datagrid1.Items[i].Cells[2].Text;
dr["solution"]=Datagrid1.Items[i].Cells[3].Text;
ds.Tables["selectItems"].Rows.Add(dr);
}
}
for(int i=0;i<Datagrid2.Items.Count;i++)
{
cb=(CheckBox)Datagrid2.Items[i].FindControl("CheckBox2");
if(cb.Checked==true)
{
dr=ds.Tables["fillblank"].NewRow();
dr["title"]=Datagrid2.Items[i].Cells[1].Text;
dr["question"]=Datagrid2.Items[i].Cells[2].Text;
dr["solution"]=Datagrid2.Items[i].Cells[3].Text;
ds.Tables["fillblank"].Rows.Add(dr);
}
}
for(int i=0;i<Datagrid3.Items.Count;i++)
{
cb=(CheckBox)Datagrid3.Items[i].FindControl("CheckBox3");
if(cb.Checked==true)
{
dr=ds.Tables["essay"].NewRow();
dr["title"]=Datagrid3.Items[i].Cells[1].Text;
dr["question"]=Datagrid3.Items[i].Cells[2].Text;
dr["solution"]=Datagrid3.Items[i].Cells[3].Text;
ds.Tables["essay"].Rows.Add(dr);
}
}
//Label1.Text=ds.Tables["selectItems"].Rows[23]["question"].ToString();
// ds.Tables.Add(dt1);
// ds.Tables.Add(dt2);
// ds.Tables.Add(dt3);
//
// Datagrid1.DataSource = ds.Tables["selectItems"].DefaultView;
// Datagrid1.DataBind();
// Response.End();
//SqlConnection cn =new SqlConnection("server=(local);uid=sa;pwd=51aspx;database=examonline");
sql1="insert into selectItems (exam_id,createTime,title,question,solution) VALUES ("+temp+",'"+DateTime.Now+"',@title,@question,@solution)";
sql2="insert into fillblank (exam_id,createTime,title,question,solution) VALUES ("+temp+",'"+DateTime.Now+"',@title,@question,@solution)";
sql3="insert into essay (exam_id,createTime,title,question,solution) VALUES ("+temp+",'"+DateTime.Now+"',@title,@question,@solution)";
da1.InsertCommand=new SqlCommand(sql1,cn);
da2.InsertCommand=new SqlCommand(sql2,cn);
da3.InsertCommand=new SqlCommand(sql3,cn);
SqlParameter param=new SqlParameter();
param = da1.InsertCommand.Parameters.Add(new SqlParameter("@title",SqlDbType.Char,50));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "title";
param = da1.InsertCommand.Parameters.Add(new SqlParameter("@question",SqlDbType.NChar,500));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "question";
param = da1.InsertCommand.Parameters.Add(new SqlParameter("@solution",SqlDbType.Char,50));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "solution";
param = da2.InsertCommand.Parameters.Add(new SqlParameter("@title",SqlDbType.Char,50));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "title";
param = da2.InsertCommand.Parameters.Add(new SqlParameter("@question",SqlDbType.NChar,500));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "question";
param = da2.InsertCommand.Parameters.Add(new SqlParameter("@solution",SqlDbType.Char,50));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "solution";
param = da3.InsertCommand.Parameters.Add(new SqlParameter("@title",SqlDbType.Char,50));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "title";
param = da3.InsertCommand.Parameters.Add(new SqlParameter("@question",SqlDbType.NChar,500));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "question";
param = da3.InsertCommand.Parameters.Add(new SqlParameter("@solution",SqlDbType.Char,50));
param.SourceVersion = DataRowVersion.Current;
param.SourceColumn = "solution";
try
{
cn.Open();
da1.Update(ds,"selectItems");
da2.Update(ds,"fillblank");
da3.Update(ds,"essay");
}
catch(Exception ex)
{
Label1.Text="数据库错误:" +ex.Message.ToString();
}
finally
{
cn.Close();
Response.Redirect("showexam.aspx");
}
}
private string HTMLEncode(string fString)
{
if(fString!=null)
{
fString = fString.Replace( ">", ">");
fString = fString.Replace("<", "<");
fString = fString.Replace( " ", " ");
fString = fString.Replace( "\"", """);
fString = fString.Replace( "\r", "");
fString = fString.Replace( "\n" + "\n", "</P><P> ");
fString = fString.Replace( "\n", "<BR> ");
return (fString);
}
else
{
return("");
}
}
protected void Button2_Click(object sender, System.EventArgs e)
{
Bindgrid(TextBox1.Text);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -