通过用户选择多个条件来进生相应的查找

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class All_Score : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)            //跳走
        {
            Server.Transfer("MangerAdmin.aspx");
        }
        else
        {
            Label3.Text = "";
            if (!IsPostBack)
            {
                DataTable dt = Class1.exetable("select * from record");
                Table_Records(dt);    //表格创建
            }
        }
        
    }

    private void Table_Records(DataTable dt)
    {
      
        Cteate_Title();          //
        TableRow row = new TableRow();
        TableCell cell = new TableCell();

        foreach (DataRow item in dt.Rows)
        {
            row = new TableRow();
            for (int i = 1; i <= 7; i++)
            {
                cell = new TableCell();
                cell.Text = item[i].ToString();
                row.Cells.Add(cell);
                if (i==1)
                {
                    if(!DropDownList1.Items.Contains(new ListItem(item[i].ToString())))
                    DropDownList1.Items.Add(new ListItem(item[i].ToString()));
                }
                if (i == 2)
                {
                    if (!DropDownList2.Items.Contains(new ListItem(item[i].ToString())))
                    DropDownList2.Items.Add(new ListItem(item[i].ToString()));
                }
                if(i==7)
                {
                    if (!DropDownList3.Items.Contains(new ListItem(item[i].ToString())))
                        DropDownList3.Items.Add(new ListItem(item[i].ToString()));
                }
            }
            Table1.Rows.Add(row);
        }
    }    //表格

    private void Cteate_Title()
    {
        TableRow row = new TableRow();

        TableCell cell = new TableCell();
        cell.Text = "回答人";
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "所回答的问题";
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "正确答案";
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "回答人答案";
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "分值";
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "回答日期";
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "问题日期";
        row.Cells.Add(cell);

        Table1.Rows.Add(row);
    }     //表头

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (RadioButton2.Checked)
        {
            FindRcord_Oper();             //用户自定义查找函数的调用 
        }
        else
        {

            Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('对不起!您还没有启用FindRecord');</script>");
            DataTable dt = Class1.exetable("select * from record");
            Table_Records(dt);    //表格创建
        }
       
    }

    private void FindRcord_Oper()
    {
        string checkbox = "";
        checkbox = Create_Find_cmd(checkbox);   //生成用户选中的参数
        if (checkbox.Length == 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('您没有选中!无法为您提供服务!');</script>");
        }
        else
        {
            string[] ckb = checkbox.Substring(1).Split(',');

            //string[] sql_c = sqlcmd.Split(',');         //分解字段
            string Sql_Where = "";
            foreach (string item in ckb)
            {
                Sql_Where += "and " + item + "=";             //串加器
                if (item == "answername")               // 筛选器  answername
                {
                    Sql_Where +="'"+ DropDownList1.SelectedItem.Text + "' ";
                }
                else if (item == "answertitle")         //筛选器answertitle
                {
                    Sql_Where += "'" + DropDownList2.SelectedItem.Text + "' ";
                }
                else if (item == "ZhouId")               //筛选器ZhouId
                {
                    Sql_Where += "'"+DropDownList3.SelectedItem.Text + "' ";
                }
            }
            string sqlcmd = "select * from record where " + Sql_Where.Substring(3);
            DataTable dt = Class1.exetable(sqlcmd);
            if (RadioButton2.Checked)
            {
                
                Table_Records(dt);
            }
            else
            {

                addcords(dt);
            }
        }
    }                           //用户自定义查找函数
    
    private string Create_Find_cmd(string checkbox)
    {
        if (CheckBox1.Checked)
        {
            checkbox += ",answername";
        }
        if (CheckBox2.Checked)
        {
            checkbox += ",answertitle";
        }
        if (CheckBox3.Checked)
        {
            checkbox += ",ZhouId";
        }
      
        return checkbox;
    }          //创建查找字段对像


    protected void Button2_Click(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
           FindRcord_Oper();     
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('对不起!您还没有启用FindScore');</script>");
            DataTable dt = Class1.exetable("select * from record");
            Table_Records(dt);    //表格创建
        }
        //DataTable dt = Class1.exetable("select * from record where answername='"+DropDownList1.SelectedItem.Text+"'");
        // addcords(dt);
    }

    //private void Oper_One()
    //{
    //    string checkbox = "";
    //    checkbox = Create_Find_cmd(checkbox);   //生成用户选中的参数
    //    if (checkbox.Length == 0)
    //    {
    //        Page.ClientScript.RegisterClientScriptBlock(GetType(), "OK", "<script>alert('您没有选中!无法为您提供服务!');</script>");
    //    }
    //    else
    //    {
    //        string[] ckb = checkbox.Substring(1).Split(',');

    //        //string[] sql_c = sqlcmd.Split(',');         //分解字段
    //        string Sql_Where = "";
    //        foreach (string item in ckb)
    //        {
    //            Sql_Where += "and " + item + "=";             //串加器
    //            if (item == "answername")               // 筛选器  answername
    //            {
    //                Sql_Where += DropDownList1.SelectedItem.Text + " ";
    //            }
    //            else if (item == "answertitle")         //筛选器answertitle
    //            {
    //                Sql_Where += "'" + DropDownList2.SelectedItem.Text + "' ";
    //            }
    //            else if (item == "ZhouId")               //筛选器ZhouId
    //            {
    //                Sql_Where += DropDownList3.SelectedItem.Text + " ";
    //            }
    //        }
    //        string sqlcmd = "select * from record where " + Sql_Where.Substring(3);
    //        DataTable dt = Class1.exetable(sqlcmd);
    //        Table_Records(dt);
    //    }
    
    
    
    //}


    private void addcords(DataTable dt)
    {

        Table table = new Table();
        int num = 0;
        createtable_title(table);             //创建表头
        foreach (DataRow item in dt.Rows)
        {
            if (!DropDownList1.Items.Contains(new ListItem(item[7].ToString())))
            {
                DropDownList1.Items.Add(new ListItem(item[7].ToString()));
            }

            TableRow row = new TableRow();
            bool sign = false;
            string score = "0";
            for (int i = 1; i < 7; i++)
            {
                TableCell cell = new TableCell();
                cell.Text = item[i].ToString();
                cell.Width = 100;
                if (i == 2)
                {
                    cell.Width = 300;
                }
                row.Cells.Add(cell);
                if (i == 5)
                {
                    if (item[4].ToString() == item[3].ToString().Trim())
                    {
                        cell = new TableCell();
                        cell.Text = "正确";
                        cell.ForeColor = System.Drawing.Color.Blue;
                        row.Cells.Add(cell);
                        sign = true;
                        score = item[5].ToString();    //存分数
                    }
                    else
                    {
                        cell = new TableCell();
                        cell.Text = "错误";
                        cell.ForeColor = System.Drawing.Color.Red;
                        row.Cells.Add(cell);
                        score = "0";
                    }
                }
                if (i == 6)
                {
                    if (sign)
                    {
                        cell = new TableCell();
                        cell.Text = score;
                        num += Convert.ToInt32(score);
                        cell.ForeColor = System.Drawing.Color.Black;
                        row.Cells.Add(cell);

                    }
                    else
                    {
                        cell = new TableCell();
                        cell.Text = score;
                        cell.ForeColor = System.Drawing.Color.Red;
                        row.Cells.Add(cell);

                    }
                }
            }
           Table1.Rows.Add(row);
            Label3.Text = num.ToString();
        }
    }                                  //添加记录

    private void createtable_title(Table table)
    {
        TableRow row = new TableRow();

        TableCell cell = new TableCell();
        cell.Text = "回答者";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "问  题";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "正确答案";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "您的回答";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "问题分数";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "回答情况";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "回答日期";
        cell.Width = 50;
        row.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "得分情况";
        cell.Width = 50;
        row.Cells.Add(cell);

        row.ForeColor = System.Drawing.Color.Brown;
        row.Font.Bold = true;
        //table.Rows.Add(row);
        //table.Width = 800;

        Table1.Rows.Add(row);
    }    

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值