private bool Valid()
 {
     DbTrans trans = new DbTrans();
     trans.CreateTransaction();
     bool ret = true;
     for (int m = 0; m < gvSection.Rows.Count; m++)
     {
         GridView gvQuestion = (GridView)gvSection.Rows[m].Cells[1].FindControl("gvQuestion");
         for (int i = 0; i < gvQuestion.Rows.Count; i++)
         {
             Label lblChoiceTypeID = (Label)gvQuestion.Rows[i].Cells[1].FindControl("lblChoiceTypeID");
             Label lblQuestionName = (Label)gvQuestion.Rows[i].Cells[2].FindControl("lblQuestionName");
             long QuestionID = Convert.ToInt64(gvQuestion.Rows[i].Cells[0].Text);
             QuestionnaireENG eng = new QuestionnaireENG();
             QuestionnaireQuestionsPara QuestionPara = eng.GetQuestionQuestionPara(QuestionID,trans);
             if (QuestionPara.IS_REQUIRE == 'Y')
             {
                 if (lblChoiceTypeID.Text == "1")
                 {
                     //Section
                     long SectionID = Convert.ToInt64(gvSection.Rows[m].Cells[0].Text);
                     if (SectionID == 1)
                     {
                         RadioButtonList rdiChoice = (RadioButtonList)gvQuestion.Rows[i].Cells[2].FindControl("rdiChoice");
                         if (rdiChoice.SelectedValue == "")
                         {
                             //แบบหลายตัวเลือก ตอบได้ 1 ข้อ สำหรับส่วนที่ 1
                             ret = false;
                             Config.SetAlert("กรุณาเลือกคำตอบสำหรับ " + lblQuestionName.Text, this);
                         }
                         else {
                             //กรณีเลือกอื่นๆ ถ้าไม่ได้ระบุอื่นๆ มาให้ Alert
                             DataTable dt = eng.GetChoiceList(QuestionID, trans);
                             foreach (DataRow dr in dt.Rows) {
                                 if (dr["is_other"].ToString() == "Y" && rdiChoice.SelectedValue==dr["id"])
                                 {
                                     TextBox txtRadioChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtRadioChoice");
                                     if (txtRadioChoice.Text.Trim() == "") {
                                         ret = false;
                                         Config.SetAlert("กรุณาเลือกคำตอบสำหรับ " + lblQuestionName.Text, this,txtRadioChoice.ClientID);
                                     }
                                 }
                             }
                         }
                     }
                     else if (SectionID == 2)
                     {
                         RadioButtonList rdiSec2Choice = (RadioButtonList)gvQuestion.Rows[i].Cells[3].FindControl("rdiSec2Choice");
                         if (rdiSec2Choice.SelectedValue == "")
                         {
                             //แบบหลายตัวเลือก ตอบได้ 1 ข้อ สำหรับส่วนที่ 2
                             ret = false;
                             Config.SetAlert("กรุณาเลือกคำตอบสำหรับ " + lblQuestionName.Text, this);
                         }
                     }
                 }
                 else if (lblChoiceTypeID.Text == "2")
                 {
                     //แบบหลายตัวเลือก ตอบได้มากกว่า 1 ข้อ
                     CheckBoxList chkChoice = (CheckBoxList)gvQuestion.Rows[i].Cells[2].FindControl("chkChoice");
                     bool isSelect = false;
                     for (int j = 0; j < chkChoice.Items.Count; j++)
                     {
                         if (chkChoice.Items[j].Selected)
                         {
                             QuestionnaireQuestionsChoicePara qqc = new QuestionnaireQuestionsChoicePara();
                             qqc = eng.GetQuestionnaireQuestionChoicePara(Convert.ToInt64(chkChoice.Items[j].Value), trans);
                             if (qqc.IS_OTHER == 'Y')
                             {
                                 TextBox txtCheckBoxChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtCheckBoxChoice");
                                 if (txtCheckBoxChoice.Text.Trim() != "")
                                     isSelect = true;
                                 else
                                 {
                                     isSelect = false;
                                     break;
                                 }
                             }
                             else
                                 isSelect = true;
                         }
                     }
                     if (isSelect == false)
                     {
                         ret = false;
                         Config.SetAlert("กรุณาเลือกคำตอบสำหรับ " + lblQuestionName.Text, this);
                     }
                 }
                 else if (lblChoiceTypeID.Text == "3")
                 {
                     //แบบให้ความเห็น ระบุเป็นข้อความ
                     TextBox txtChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtChoice");
                     if (txtChoice.Text.Trim() == "")
                     {
                         ret = false;
                         Config.SetAlert("กรุณาเลือกคำตอบสำหรับ " + lblQuestionName.Text, this);
                     }
                 }
                 else if (lblChoiceTypeID.Text == "4")
                 {
                     //แบบให้ความเห็น ระบุเป็นตัวเลข
                     TextBox txtChoice = (TextBox)gvQuestion.Rows[i].Cells[2].FindControl("txtChoice");
                     if (txtChoice.Text.Trim() == "")
                     {
                         ret = false;
                         Config.SetAlert("กรุณาเลือกคำตอบสำหรับ " + lblQuestionName.Text, this);
                     }
                 }
             }
         }
     }
     trans.CommitTransaction();
     return ret;
 }
    protected void cmbChoiceTypeID_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbChoiceTypeID.SelectedValue == "1" || cmbChoiceTypeID.SelectedValue == "2") {
            Panel3.Visible = true;
            lblChoceTypeText.Text = cmbChoiceTypeID.SelectedText;
            DbTrans trans = new DbTrans();
            trans.CreateTransaction();

            QuestionnaireENG eng = new QuestionnaireENG();
            DataTable dt = eng.GetChoiceList(Convert.ToInt64(txtQuestionID.Text), trans);
            //DataTable dt = eng.GetChoiceList(1, trans);
            if (dt.Rows.Count > 0) {
                gvChoiceList.DataSource = dt;
                gvChoiceList.DataBind();
            }
            trans.CommitTransaction();
        }else
            Panel3.Visible = false;
    }
    protected void gvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (IsPostBack == false)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DbTrans trans = new DbTrans();
                trans.CreateTransaction();
                long QuestionID = Convert.ToInt64(e.Row.Cells[0].Text);
                QuestionnaireENG eng = new QuestionnaireENG();
                QuestionnaireQuestionsPara para = eng.GetQuestionQuestionPara(QuestionID, trans);
                QuestionnaireSectionPara secPara = eng.GetQuestionnaireSectionPara(para.QUESTIONNAIRE_SECTION_ID, trans);

                if (secPara.SECTION_TYPE_ID == 1)
                {
                    if (para.CHOICE_TYPE_ID == 1)
                    {
                        DataTable dt = eng.GetChoiceList(QuestionID, trans);
                        DataTable dtConfig = eng.GetQuestionConfig(QuestionID, trans);
                        if (dt.Rows.Count > 0)
                        {
                            RadioButtonList rdiChoice = (RadioButtonList)e.Row.Cells[2].FindControl("rdiChoice");
                            foreach (DataRow dr in dtConfig.Rows)
                            {
                                if (dr["config_name"].ToString() == "RepeatDirection")
                                {
                                    rdiChoice.RepeatDirection = (dr["config_value"].ToString() == "Horizontal" ? RepeatDirection.Horizontal : RepeatDirection.Vertical);
                                }
                            }

                            rdiChoice.Visible = true;
                            rdiChoice.DataTextField = "choice_name";
                            rdiChoice.DataValueField = "id";
                            rdiChoice.DataSource = dt;
                            rdiChoice.DataBind();
                            //rdiChoice.SelectedValue=

                            Label lblTextChoiceName = (Label)e.Row.Cells[2].FindControl("lblTextChoiceName");

                            int j = 0;
                            foreach (DataRow dr in dt.Rows)
                            {
                                if (dr["is_other"].ToString() == "Y")
                                {
                                    TextBox txtRadioChoice = (TextBox)e.Row.Cells[2].FindControl("txtRadioChoice");
                                    txtRadioChoice.Visible = true;
                                    rdiChoice.AutoPostBack = true;
                                }
                                if (dr["is_default"].ToString() == "Y") {
                                    rdiChoice.Items[j].Selected = true;
                                }
                                j++;
                            }
                        }
                    }
                    else if (para.CHOICE_TYPE_ID == 2) {
                        DataTable dt = eng.GetChoiceList(QuestionID, trans);
                        if (dt.Rows.Count > 0)
                        {
                            CheckBoxList chkChoice = (CheckBoxList)e.Row.Cells[2].FindControl("chkChoice");
                            chkChoice.Visible = true;
                            chkChoice.DataTextField = "choice_name";
                            chkChoice.DataValueField = "id";
                            chkChoice.DataSource = dt;
                            chkChoice.DataBind();

                            int j = 0;
                            foreach (DataRow dr in dt.Rows)
                            {
                                if (dr["is_other"].ToString() == "Y")
                                {
                                    TextBox txtCheckBoxChoice = (TextBox)e.Row.Cells[2].FindControl("txtCheckBoxChoice");
                                    txtCheckBoxChoice.Visible = true;
                                    chkChoice.AutoPostBack = true;
                                }
                                if (dr["is_default"].ToString() == "Y")
                                {
                                    chkChoice.Items[j].Selected = true;
                                }
                                j++;
                            }
                        }
                    }
                    else if (para.CHOICE_TYPE_ID == 3) {
                        TextBox txtChoice = (TextBox)e.Row.Cells[2].FindControl("txtChoice");
                        txtChoice.Visible = true;

                        DataTable dt = eng.GetChoiceList(QuestionID, trans);
                        if (dt.Rows.Count > 0)
                        {
                            Label lblTextChoiceName = (Label)e.Row.Cells[2].FindControl("lblTextChoiceName");
                            lblTextChoiceName.Visible = true;
                            lblTextChoiceName.Text = dt.Rows[0]["choice_name"].ToString();
                        }
                    }
                    else if (para.CHOICE_TYPE_ID == 4)
                    {
                        TextBox txtChoice = (TextBox)e.Row.Cells[2].FindControl("txtChoice");
                        txtChoice.Visible = true;

                        DataTable dt = eng.GetChoiceList(QuestionID, trans);
                        if (dt.Rows.Count > 0)
                        {
                            Label lblTextChoiceName = (Label)e.Row.Cells[2].FindControl("lblTextChoiceName");
                            lblTextChoiceName.Visible = true;
                            lblTextChoiceName.Text = dt.Rows[0]["choice_name"].ToString();
                        }
                    }
                }
                else if (secPara.SECTION_TYPE_ID == 2)
                {
                    RadioButtonList rdiSec2Choice = (RadioButtonList)e.Row.Cells[3].FindControl("rdiSec2Choice");
                    rdiSec2Choice.Visible = true;
                    rdiSec2Choice.Width = 40 * Convert.ToInt16(secPara.CHOICE_QTY);
                    for (int i = 0; i < secPara.CHOICE_QTY; i++)
                    {
                        rdiSec2Choice.Items.Add(new ListItem((i + 1).ToString(), (i + 1).ToString()));
                    }
                }
                trans.CommitTransaction();
            }
        }
    }