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

namespace helpdesk.helpdesk
{
    public partial class Pages : System.Web.UI.Page
    {


        public int activeid =0;   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { 

                if (UserHelper.GetUserId > 0)
                {
                    DataTable dt = UsersHelper.GetUserInfoById(UserHelper.GetUserId);
                    truename.Text = dt.Rows[0]["displayname"].ToString();
                    dept.Text = dt.Rows[0]["department"].ToString();
                    email.Text = dt.Rows[0]["email"].ToString();
                    mobile.Text = dt.Rows[0]["tel"].ToString();
                }


            }
        }




        protected void Page_Init(object sender, EventArgs e)
        {

            bool isSingleRow = false;

            #region 1投票ID


            if (Request.QueryString["activeid"] != null)
            {
                activeid = int.Parse(Request.QueryString["activeid"]);
            }
            else
            {
                return;
            }

            #endregion




            #region 判断条件



            SqlDataReader sdr = DBHelper.Instance.ExeDataReader(" select * from survery_defineactive where id=" + activeid);
            sdr.Read();

            if (!sdr.HasRows)
            {
                sdr.Close();
                Button1.Enabled = false;
                return;
            }


            lbl_title.Text = sdr["title"].ToString();
            Header.Title = sdr["title"].ToString();
       
            DateTime begintime = DateTime.Parse(sdr["begintime"].ToString());
            DateTime endtime = DateTime.Parse(sdr["endtime"].ToString());
            bool ispublic = sdr["ispublic"].ToString() == "1" ? true : false;
            bool allowmultivote = sdr["allowmultivote"].ToString() == "1" ? true : false;
            sdr.Close();





            //2会议是否开始
            if (begintime != DateTime.Parse("1900-1-1"))
            {
                if (begintime > DateTime.Now)
                {
                    Button1.Enabled = false; 
                    Button1.Text = "提交还未开始";
                  
                }
            }


            //3会议是否结束
            if (endtime != DateTime.Parse("1900-1-1"))
            {
                if (endtime < DateTime.Now)
                { 
                    Button1.Enabled = false;
                    Button1.Text = "提交已经结束"; 
                }
            }


            //是否允许多投
            if (!allowmultivote)
            {
                
                int count = int.Parse(DBHelper.Instance.ExeScalar(" select count(*) from survery_lists where uid=" + UserHelper.GetUserId + " and activeid=" + activeid).ToString());
                if (count > 0)
                {
                    Button1.Enabled = false;
                    Button1.Text = "您已经提交";
                    
                }

            }



            #endregion




            string sql = @"SELECT  
             distinct  
             id,  col, mustfilled,
            types,  isSingleRow,
            subject , bad,[mid],good,coin FROM       
            survery_subjects  
            where activeid=" + activeid;




            DataSet ds = DBHelper.Instance.ExeDataSet(sql);





            int jj = 0;
            int cc = ds.Tables[0].Rows.Count;


            for (int ii = 0; ii < cc; ii++)
            {

                jj = ii + 1;

                DataRow dr = ds.Tables[0].Rows[ii];

                string types = dr["types"].ToString();
                int col = int.Parse(dr["col"].ToString());
                string subjects = dr["subject"].ToString();
                string id = dr["id"].ToString();
                string ctrlid = "ctrl" + id;
                bool mustfilled = dr["mustfilled"].ToString() == "1" ? true : false;
                isSingleRow = dr["IsSingleRow"].ToString() == "1" ? true : false;

                string badword = dr["bad"].ToString();
                string midword = dr["mid"].ToString();
                string goodword = dr["good"].ToString();
                int coin = int.Parse(dr["coin"].ToString());


                if (isSingleRow)
                {
                    AddSingleLine(id, types, subjects, ctrlid, mustfilled, col, badword, midword, goodword, coin);

                }
                else
                {

                    if (jj <= cc - 1)
                    {

                        #region 处理单元格合并



                        DataRow drNext = ds.Tables[0].Rows[jj];

                        if (drNext["IsSingleRow"].ToString() == "0")
                        {

                            #region 合并一行



                            Literal trStart = new Literal();
                            trStart.Text = "<tr> ";
                            content.Controls.Add(trStart);


                            AddCell(id, types, subjects, ctrlid, mustfilled, col);


                            AddCell(
                                drNext["id"].ToString(),
                                drNext["types"].ToString(),
                                drNext["subject"].ToString(),
                                "ctrl" + drNext["id"].ToString(),
                                (drNext["mustfilled"].ToString() == "1" ? true : false),
                                 int.Parse(drNext["col"].ToString())
                            );


                            Literal trEnd = new Literal();
                            trEnd.Text = "</tr> ";
                            content.Controls.Add(trEnd);

                            #endregion


                        }
                        else
                        {


                            #region 插入空白


                            Literal trStart = new Literal();
                            trStart.Text = "<tr> ";
                            content.Controls.Add(trStart);


                            AddCell(id, types, subjects, ctrlid, mustfilled, col);


                            Literal trEnd = new Literal();
                            trEnd.Text = " <td>&nbsp;</td><td>&nbsp;</td></tr> ";
                            content.Controls.Add(trEnd);


                            AddSingleLine(
                                  drNext["id"].ToString(),
                                  drNext["types"].ToString(),
                                  drNext["subject"].ToString(),
                                  "ctrl" + drNext["id"].ToString(),
                                  (drNext["mustfilled"].ToString() == "1" ? true : false),
                                   int.Parse(drNext["col"].ToString()),
                                    badword, midword, goodword, coin
                              );

                            #endregion



                        }



                        #endregion


                    }
                    else
                    {

                        //最后一行


                        DataRow drNext = ds.Tables[0].Rows[jj - 1];

                        if (drNext["IsSingleRow"].ToString() == "0")
                        {

                            #region 合并一行



                            Literal trStart = new Literal();
                            trStart.Text = "<tr> ";
                            content.Controls.Add(trStart);




                            AddCell(
                                  drNext["id"].ToString(),
                                  drNext["types"].ToString(),
                                  drNext["subject"].ToString(),
                                  "ctrl" + drNext["id"].ToString(),
                                  (drNext["mustfilled"].ToString() == "1" ? true : false),
                                   int.Parse(drNext["col"].ToString())
                              );


                            Literal trEnd = new Literal();
                            trEnd.Text = " <td>&nbsp;</td><td>&nbsp;</td></tr> ";
                            content.Controls.Add(trEnd);




                            #endregion


                        }
                        else
                        {


                            #region 插入一行


                            AddSingleLine(
                                    drNext["id"].ToString(),
                                    drNext["types"].ToString(),
                                    drNext["subject"].ToString(),
                                    "ctrl" + drNext["id"].ToString(),
                                    (drNext["mustfilled"].ToString() == "1" ? true : false),
                                     int.Parse(drNext["col"].ToString()),
                                      badword, midword, goodword, coin
                                );

                            #endregion



                        }





                    }

                    ii++;



                }





            }

        }











        public void AddSingleLine(string id, string types, string subjects, string ctrlid, bool mustfilled, int col, string badword, string midword, string goodword, int coin)
        {


            switch (types)
            {

                #region 单行


                case "TextBox":
                case "Date":
                case "MultiTextBox":
                    {

                        #region TextBox
                        Literal lbl = new Literal();
                        lbl.Text = "  <tr> <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td class=tdValue colSpan=3> ";
                        content.Controls.Add(lbl);
                        TextBox c = new TextBox();
                        c.ID = ctrlid;
                        if (types == "MultiTextBox")
                        {
                            c.TextMode = TextBoxMode.MultiLine;
                            c.CssClass = "form-control";
                        }
                        else if (types == "Date")
                        {
                            c.Attributes.Add("onclick", "javascript:ShowCalendar(this)");
                            c.CssClass = "form-control";
                        }
                        else
                        {
                            c.CssClass = "form-control";

                        }
                        content.Controls.Add(c);



                        if (mustfilled)
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv_" + ctrlid;
                            rfv.ControlToValidate = ctrlid;
                            rfv.ErrorMessage = "*";
                            rfv.Display = ValidatorDisplay.Dynamic;
                            rfv.ForeColor = System.Drawing.Color.Red;
                            content.Controls.Add(rfv);
                        }



                        Literal lblend = new Literal();
                        lblend.Text = "</td></tr> ";
                        content.Controls.Add(lblend);
                        break;


                        #endregion

                    }



                case "Label":
                    {

                        #region Label



                        Literal lbl = new Literal();
                        lbl.Text = "  <tr > <td class=tdLabel>&nbsp;</td>  <td  class=tdValue  colSpan=3> ";
                        content.Controls.Add(lbl);


                        Label lb = new Label();
                        lb.ID = ctrlid;
                        lb.Text = subjects;
                        lb.CssClass = "form-control";
                        content.Controls.Add(lb);
                        Literal lblend = new Literal();
                        lblend.Text = "</td></tr> ";
                        content.Controls.Add(lblend);
                        break;

                        #endregion

                    }




                case "CheckBoxList":
                    {

                        #region CheckBoxList


                        Literal lbl = new Literal();
                        lbl.Text = "  <tr> <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue  colSpan=3> ";


                        content.Controls.Add(lbl);
                        CheckBoxList cbl = new CheckBoxList();
                        cbl.RepeatColumns = col;
                        cbl.CssClass = "votecheckboxlist";
                        cbl.RepeatDirection = RepeatDirection.Horizontal;
                        cbl.ID = ctrlid;
                        cbl.ClientIDMode = ClientIDMode.Static;
                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr1 = c.ExecuteReader();
                        while (dr1.Read())
                        {
                            ListItem li = new ListItem(dr1["item"].ToString());
                            cbl.Items.Add(li);
                        }
                        con.Close();
                        content.Controls.Add(cbl);



                        if (mustfilled)
                        {
                            CustomValidator rfv = new CustomValidator();
                            rfv.ID = "Validator_" + ctrlid;
                            rfv.ErrorMessage = "*";
                            rfv.Display = ValidatorDisplay.Dynamic;
                            rfv.ClientValidationFunction = "CheckItem";
                            rfv.ForeColor = System.Drawing.Color.Red;
                            content.Controls.Add(rfv);
                        }


                        Literal lblend = new Literal();
                        lblend.Text = "</td></tr> ";
                        content.Controls.Add(lblend);



                        break;


                        #endregion


                    }



                case "DropDownList":
                    {
                        #region DropDownList



                        Literal lbl = new Literal();
                        lbl.Text = "  <tr> <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue  colSpan=3> ";


                        content.Controls.Add(lbl);

                        DropDownList ddl = new DropDownList();
                        ddl.CssClass = "votedowndownlist";
                        ddl.ID = ctrlid;

                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr1 = c.ExecuteReader();
                        while (dr1.Read())
                        {
                            ListItem li = new ListItem(dr1["item"].ToString());
                            ddl.Items.Add(li);
                        }
                        con.Close();
                        content.Controls.Add(ddl);




                        Literal lblend = new Literal();
                        lblend.Text = "</td></tr> ";


                        content.Controls.Add(lblend);


                        break;

                        #endregion

                    }


                case "RadioButtonList":
                    {
                        #region RadioButtonList



                        Literal lbl = new Literal();
                        lbl.Text = "  <tr> <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue   colSpan=3> ";
                        content.Controls.Add(lbl);


                        RadioButtonList rbl = new RadioButtonList();
                        rbl.ID = ctrlid;
                        rbl.RepeatColumns = col;

                        rbl.CssClass = "voteradiobuttonlist";
                        rbl.RepeatDirection = RepeatDirection.Horizontal;
                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr2 = c.ExecuteReader();
                        while (dr2.Read())
                        {
                            ListItem li = new ListItem(dr2["item"].ToString());
                            rbl.Items.Add(li);
                        }
                        con.Close();

                        content.Controls.Add(rbl);




                        if (mustfilled)
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv_" + ctrlid;
                            rfv.ControlToValidate = ctrlid;
                            rfv.ErrorMessage = "*";
                            rfv.Display = ValidatorDisplay.Dynamic;
                            rfv.ForeColor = System.Drawing.Color.Red;
                            content.Controls.Add(rfv);
                        }



                        Literal lblend = new Literal();
                        lblend.Text = "   </td></tr> ";
                        content.Controls.Add(lblend);
                        break;

                        #endregion
                    }



                case "FileUpload":
                    {
                        #region FileUpLoad



                        Literal lbl = new Literal();
                        lbl.Text = "  <tr> <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue   colSpan=3> ";
                        content.Controls.Add(lbl);


                        Label lb = new Label();
                        lb.ID = ctrlid;
                        lb.ClientIDMode = ClientIDMode.Static;
                        lb.Text = "<div style='display:none' id=copy_" + ctrlid + "></div><p><a href=# onclick=\"window.open('../SimlpeUploadFile.aspx?t=customlist&cid=" + ctrlid + "','','width=700px,height=350px,left=200px,top=100px');return false;\">上传</a></p>";
                        lb.CssClass = "upload";
                        content.Controls.Add(lb);

                        HiddenField hf_attach = new HiddenField();
                        hf_attach.ID = "hfatta_" + ctrlid;
                        hf_attach.ClientIDMode = ClientIDMode.Static;
                        content.Controls.Add(hf_attach);


                        Literal lblend = new Literal();
                        lblend.Text = "</td></tr> ";
                        content.Controls.Add(lblend);





                        break;

                        #endregion
                    }


                case "GroupOption":
                    {


                        //BUILD HEADER

                        string table = "";

                        string row = "";
                        string tblHead = "";
                        string tblSubHead = "";

                        int milPos = (coin + 1) / 2;




                        #region Head


                        tblHead += "<tr>";

                        tblHead += "<th>&nbsp;</th>";
                        tblHead += "<th class=head1>&nbsp;</th>";
                        for (int kk = 1; kk <= coin; kk++)
                        {
                            if (kk == 1)
                            {
                                tblHead += "<th>" + badword + "</th>";

                            }
                            else if (kk == milPos)
                            {

                                tblHead += "<th>" + midword + "</th>";

                            }

                            else if (kk == coin)
                            {
                                tblHead += "<th>" + goodword + "</th>";

                            }
                            else
                            {

                                tblHead += "<th>&nbsp;</th>";
                            }

                        }



                        tblHead += "</tr>";



                        #endregion


                        #region SubHead

                        tblSubHead = "<tr>";
                        tblSubHead += "<td>&nbsp;</td>";
                        tblSubHead += "<td class=cell1 >&nbsp;</td>";
                        for (int ll = 1; ll <= coin; ll++)
                        {

                            tblSubHead += "<td>" + ll + "</td>";
                        }

                        tblSubHead += "</tr>";

                        #endregion


                        table = "<table class='grouption'>" + tblHead + tblSubHead;

                        //获取选项

                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr2 = c.ExecuteReader();


                        string tempRow = "";
                        while (dr2.Read())
                        {

                            tempRow = "";
                            tempRow = "<tr>";
                            tempRow += "<td>" + dr2["item"] + "</td>";
                            tempRow += "<td class=cell1>&nbsp;</td>";

                            for (int ll = 1; ll <= coin; ll++)
                            {

                                tempRow += "<td> <input type=radio value=" + ll + "  name=groupoption" + dr2["itemid"].ToString() + " > </td>";
                            }
                            tempRow += "</tr>";
                            table += tempRow;

                        }
                        con.Close();



                        table += "</table>";







                        Literal lbl = new Literal();
                        lbl.Text = "  <tr> <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue   colSpan=3> ";
                        content.Controls.Add(lbl);



                        Literal litable = new Literal();
                        litable.Text = table;
                        content.Controls.Add(litable);


                        Literal lblend = new Literal();
                        lblend.Text = "   </td></tr> ";
                        content.Controls.Add(lblend);

                        break;


                    }





                #endregion

            }
        }







        public void AddCell(string id, string types, string subjects, string ctrlid, bool mustfilled, int col)
        {
            switch (types)
            {

                #region 单元格


                case "TextBox":
                case "Date":
                case "MultiTextBox":
                    {

                        #region TextBox
                        Literal lbl = new Literal();
                        lbl.Text = "  <td class=tdLabel >" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td class=gwTdInput> ";
                        content.Controls.Add(lbl);
                        TextBox c = new TextBox();
                        c.ID = ctrlid;
                        if (types == "MultiTextBox")
                        {
                            c.TextMode = TextBoxMode.MultiLine;
                            c.CssClass = "votemultitextbox";
                        }
                        else if (types == "Date")
                        {
                            c.Attributes.Add("onclick", "javascript:ShowCalendar(this)");
                            c.CssClass = "votedate";
                        }
                        else
                        {
                            c.CssClass = "votetextbox";

                        }
                        content.Controls.Add(c);



                        if (mustfilled)
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv_" + ctrlid;
                            rfv.ControlToValidate = ctrlid;
                            rfv.ErrorMessage = "*";
                            rfv.Display = ValidatorDisplay.Dynamic;
                            rfv.ForeColor = System.Drawing.Color.Red;
                            content.Controls.Add(rfv);
                        }



                        Literal lblend = new Literal();
                        lblend.Text = "</td> ";
                        content.Controls.Add(lblend);
                        break;


                        #endregion

                    }



                case "Label":
                    {

                        #region Label



                        Literal lbl = new Literal();
                        lbl.Text = "  <td class=gwTdLabel  colspan=2> ";
                        content.Controls.Add(lbl);


                        Label lb = new Label();
                        lb.ID = ctrlid;
                        lb.Text = subjects;
                        lb.CssClass = "votelabel";
                        content.Controls.Add(lb);
                        Literal lblend = new Literal();
                        lblend.Text = "</td> ";
                        content.Controls.Add(lblend);
                        break;

                        #endregion

                    }




                case "CheckBoxList":
                    {

                        #region CheckBoxList


                        Literal lbl = new Literal();
                        lbl.Text = "  <td class=tdLabel >" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue> ";


                        content.Controls.Add(lbl);
                        CheckBoxList cbl = new CheckBoxList();
                        cbl.RepeatColumns = col;
                        cbl.CssClass = "votecheckboxlist";
                        cbl.RepeatDirection = RepeatDirection.Horizontal;
                        cbl.ID = ctrlid;
                        cbl.ClientIDMode = ClientIDMode.Static;
                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr1 = c.ExecuteReader();
                        while (dr1.Read())
                        {
                            ListItem li = new ListItem(dr1["item"].ToString());
                            cbl.Items.Add(li);
                        }
                        con.Close();
                        content.Controls.Add(cbl);



                        if (mustfilled)
                        {
                            CustomValidator rfv = new CustomValidator();
                            rfv.ID = "Validator_" + ctrlid;
                            rfv.ErrorMessage = "*";
                            rfv.Display = ValidatorDisplay.Dynamic;
                            rfv.ClientValidationFunction = "CheckItem";
                            rfv.ForeColor = System.Drawing.Color.Red;
                            content.Controls.Add(rfv);
                        }


                        Literal lblend = new Literal();
                        lblend.Text = "</td> ";
                        content.Controls.Add(lblend);



                        break;


                        #endregion


                    }



                case "DropDownList":
                    {
                        #region DropDownList



                        Literal lbl = new Literal();
                        lbl.Text = "   <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue> ";


                        content.Controls.Add(lbl);

                        DropDownList ddl = new DropDownList();
                        ddl.CssClass = "votedowndownlist";
                        ddl.ID = ctrlid;

                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr1 = c.ExecuteReader();
                        while (dr1.Read())
                        {
                            ListItem li = new ListItem(dr1["item"].ToString());
                            ddl.Items.Add(li);
                        }
                        con.Close();
                        content.Controls.Add(ddl);




                        Literal lblend = new Literal();
                        lblend.Text = "</td> ";


                        content.Controls.Add(lblend);


                        break;

                        #endregion

                    }


                case "RadioButtonList":
                    {
                        #region RadioButtonList




                        Literal lbl = new Literal();
                        lbl.Text = " <td class=gwTdLabel >" + subjects + (mustfilled ? "<font color=red>*</font>" : "") + "</td>  <td  class=tdValue> ";
                        content.Controls.Add(lbl);


                        RadioButtonList rbl = new RadioButtonList();
                        rbl.ID = ctrlid;
                        rbl.RepeatColumns = col;

                        rbl.CssClass = "voteradiobuttonlist";
                        rbl.RepeatDirection = RepeatDirection.Horizontal;
                        SqlCommand c = new SqlCommand();
                        SqlConnection con = new SqlConnection(Helper.GetCon());
                        c.Connection = con;
                        c.CommandText = "select  itemid,  id, item from  survery_options where activeid=" + activeid + " and id=" + id;
                        con.Open();
                        SqlDataReader dr2 = c.ExecuteReader();
                        while (dr2.Read())
                        {
                            ListItem li = new ListItem(dr2["item"].ToString());
                            rbl.Items.Add(li);

                        }
                        con.Close();

                        content.Controls.Add(rbl);




                        if (mustfilled)
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv_" + ctrlid;
                            rfv.ControlToValidate = ctrlid;
                            rfv.ErrorMessage = "*";
                            rfv.Display = ValidatorDisplay.Dynamic;
                            rfv.ForeColor = System.Drawing.Color.Red;
                            content.Controls.Add(rfv);
                        }



                        Literal lblend = new Literal();
                        lblend.Text = "   </td> ";
                        content.Controls.Add(lblend);
                        break;

                        #endregion
                    }



                case "FileUpload":
                    {
                        #region FileUpLoad



                        Literal lbl = new Literal();
                        lbl.Text = "   <td class=tdLabel>" + subjects + (mustfilled ? "<font color=red>*</font>" : "")  + "</td>  <td  class=gwTdInput  > ";
                        content.Controls.Add(lbl);


                        Label lb = new Label();
                        lb.ID = ctrlid;
                        lb.ClientIDMode = ClientIDMode.Static;
                        lb.Text = "<div style='display:none' id=copy_" + ctrlid + "></div><p><a href=# onclick=\"window.open('../SimlpeUploadFile.aspx?t=customlist&cid=" + ctrlid + "','','width=700px,height=350px,left=200px,top=100px');return false;\">上传</a></p>";
                        lb.CssClass = "upload";
                        content.Controls.Add(lb);

                        HiddenField hf_attach = new HiddenField();
                        hf_attach.ID = "hfatta_" + ctrlid;
                        hf_attach.ClientIDMode = ClientIDMode.Static;
                        content.Controls.Add(hf_attach);


                        Literal lblend = new Literal();
                        lblend.Text = "</td> ";
                        content.Controls.Add(lblend);





                        break;

                        #endregion
                    }



                #endregion

            }
        }





















        protected void Button1_Click(object sender, EventArgs e)
        {

            string prefix = "";
            #region 1投票ID



            if (Request.QueryString["activeid"] != null)
            {
               
               
            }
            else
            {
                Helper.Result(this, "系统找不到URL传递的treeid参数");
                return;
            }
            #endregion



            Hashtable ht = new Hashtable();



            string sql = @"SELECT  
             distinct  
             id,  
            types,  
            subject FROM       
            survery_subjects  
            where activeid=" + activeid;

            DataSet ds = DBHelper.Instance.ExeDataSet(sql);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string types = dr["types"].ToString();
                string subjects = dr["subject"].ToString();
                string id = dr["id"].ToString();
                string ctrlid = "ctrl" + id;
                string key = ctrlid;
                string value = "";

                switch (types)
                {

                    case "TextBox":
                    case "Date":
                    case "MultiTextBox":
                        {

                            TextBox txt = ((TextBox)content.FindControl(ctrlid));
                            value = txt.Text;
                            break;
                        }




                    case "Label":
                        {

                            Label lbl = ((Label)content.FindControl(ctrlid));
                            value = lbl.Text;
                            break;
                        }





                    case "CheckBoxList":
                        {

                            CheckBoxList res = ((CheckBoxList)content.FindControl(ctrlid));

                            value = Helper.GetCheckBoxListText(res);





                            break;
                        }



                    case "DropDownList":
                        {

                            DropDownList ddl = ((DropDownList)content.FindControl(ctrlid));


                            foreach (ListItem li in ddl.Items)
                            {
                                if (li.Selected)
                                {
                                    value += li.Text;
                                }
                            }
                            break;
                        }


                    case "RadioButtonList":
                        {

                            RadioButtonList rbl = ((RadioButtonList)content.FindControl(ctrlid));


                            foreach (ListItem li in rbl.Items)
                            {
                                if (li.Selected)
                                {
                                    value += li.Text;
                                }
                            }


                            break;

                        }


                    case "FileUpload":
                        {
                            value = Request["hfatta_" + ctrlid];
                            //mustfild
                            DataSet _ds = DBHelper.Instance.ExeDataSet(" SELECT mustfilled, subject  FROM survery_subjects where id=" + id + " and activeid=" + activeid);

                            string _mustfilled = _ds.Tables[0].Rows[0]["mustfilled"].ToString();
                            string _subject = _ds.Tables[0].Rows[0]["subject"].ToString();
                            if (_mustfilled.ToString() == "1")
                            {
                                if (!value.ToLower().Contains("<a"))
                                {
                                    Helper.Result(this, _subject + "必须上传附件");
                                    return;
                                }
                            }


                            break;
                        }


                    case "GroupOption":
                        {

                            string templateKey = "";
                            DataSet dsGO = DBHelper.Instance.ExeDataSet(" SELECT itemid, item FROM survery_options where id=" + id + " and activeid=" + activeid);

                            foreach (DataRow drGO in dsGO.Tables[0].Rows)
                            {
                                string selectGO = Request["groupoption" + drGO["itemid"]];
                                if (string.IsNullOrEmpty(selectGO))
                                {

                                    Helper.Result(this, drGO["item"].ToString() + " 必须选择");
                                    return;
                                }

                                templateKey = templateKey + selectGO + ";";

                            }

                            value = templateKey;
                            break;
                        }





                }


                ht.Add(key, value);

            }


            //Insert Sql



            SqlCommand InsertCmd = new SqlCommand();
            SqlConnection con = new SqlConnection(Helper.GetCon());
            InsertCmd.Connection = con;



            InsertCmd.CommandText = "insert into survery_lists(treeid,uid,activeid,username,dept,email,mobile,ctrl1,ctrl2,ctrl3,ctrl4,ctrl5,ctrl6,ctrl7,ctrl8,ctrl9,ctrl10,ctrl11,ctrl12,ctrl13,ctrl14,ctrl15,ctrl16,ctrl17,ctrl18,ctrl19,ctrl20,ctrl21,ctrl22,ctrl23,ctrl24,ctrl25,ctrl26,ctrl27,ctrl28,ctrl29,ctrl30) values(@treeid,@uid,@activeid,@username,@dept,@email,@mobile,@ctrl1,@ctrl2,@ctrl3,@ctrl4,@ctrl5,@ctrl6,@ctrl7,@ctrl8,@ctrl9,@ctrl10,@ctrl11,@ctrl12,@ctrl13,@ctrl14,@ctrl15,@ctrl16,@ctrl17,@ctrl18,@ctrl19,@ctrl20,@ctrl21,@ctrl22,@ctrl23,@ctrl24,@ctrl25,@ctrl26,@ctrl27,@ctrl28,@ctrl29,@ctrl30)";
            InsertCmd.Parameters.AddWithValue("activeid", activeid);
            InsertCmd.Parameters.AddWithValue("treeid", "0");
            InsertCmd.Parameters.AddWithValue("username", truename.Text);
            InsertCmd.Parameters.AddWithValue("dept", dept.Text);
            InsertCmd.Parameters.AddWithValue("email", email.Text);
            InsertCmd.Parameters.AddWithValue("mobile", mobile.Text);
            InsertCmd.Parameters.AddWithValue("uid", UserHelper.GetUserId);
            bool tag = false;


            for (int i = 1; i < 31; i++)
            {
                tag = false;
                foreach (DictionaryEntry de in ht)
                {
                    if (de.Key.ToString() == "ctrl" + i)
                    {
                        InsertCmd.Parameters.AddWithValue(de.Key.ToString(), de.Value.ToString());
                        tag = true;
                        break;
                    }

                }

                if (!tag)
                {
                    InsertCmd.Parameters.AddWithValue("ctrl" + i, "");
                }


            }




            con.Open();
            InsertCmd.CommandText += "   ; select SCOPE_IDENTITY() ";
            object newListId = InsertCmd.ExecuteScalar();
            con.Close();
            Button1.Enabled = false;
           
           
           Page.RegisterClientScriptBlock("close", "<script>alert('操作成功');  window.opener=null;window.open('','_self'); window.close(); </script>");

          

            

        }






        public string ExeCode(string code)
        {
            return "";
//            string lcCode = code;

//            lcCode = @"
//          using System; 
//using System.Collections.Generic; 
// 
//using System.Web;  
//using System.Data; 
//using System.Data.SqlClient;  
//            namespace MyNamespace {
//            public class MyClass {
//
//            public string DynamicCode() {
//            return  " + lcCode + ";}   }    }";

//            ICodeCompiler loCompiler = new CSharpCodeProvider().CreateCompiler();
//            CompilerParameters loParameters = new CompilerParameters();
//            loParameters.ReferencedAssemblies.Add("System.dll");
//            loParameters.ReferencedAssemblies.Add("System.Web.dll");
//            loParameters.ReferencedAssemblies.Add("System.Data.dll");


//            loParameters.ReferencedAssemblies.Add(Server.MapPath("~/bin/Portal.dll"));
//            loParameters.GenerateInMemory = true;
//            CompilerResults loCompiled = loCompiler.CompileAssemblyFromSource(loParameters, lcCode);

//            if (loCompiled.Errors.HasErrors)
//            {
//                return loCompiled.Errors[0].ErrorText;
//            }
//            Assembly loAssembly = loCompiled.CompiledAssembly;
//            object loObject = loAssembly.CreateInstance("MyNamespace.MyClass");
//            return loObject.GetType().InvokeMember("DynamicCode", BindingFlags.InvokeMethod, null, loObject, null).ToString();


        }



    }
}