通過使用者選擇多個條件來進生相應的查詢

c979170768發表於2011-11-18
 
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);
    }    

}


 

相關文章