添加行的动态Q / A构建器?

时间:2018-08-27 21:45:07

标签: c# asp.net gridview

我有一个动态的表单创建者,用户可以在其中添加自己的问题和答案以及控件(文本框,下拉列表和复选框)。

但是,他们需要能够在gridview中显示问题的列标题为问题,而gridview行则包含控件。需要添加新行并填充每行的控件,然后提交到数据库。有没有人在线上看到与此类似的内容作为教程,反对这里。 示例:

  Username   UserAge
1 TextboxControl    DropdownControl addrow
1 TextboxControl    DropdownControl addrow
1 TextboxControl    DropdownControl addrow

SubmitButton
  Code
.aspx page
 <asp:GridView ID="gvQuestions" runat="server" OnRowCreated="gvQuestions_RowCreated" OnRowDataBound="gvQuestions_RowDataBound" Style="width: 780px;">
                                </asp:GridView>

隐藏代码

private void SetInitialRow()
        {
            var GetQuestionsData = new OncologyInformation().SelectOncologyQuestions(Request.QueryString["ProjectID"].ToString(), "1");
            var GetQuestionsDatabySection = from qd in GetQuestionsData where qd.SectionName == "VISIT B" select qd;

            DataTable dt = new DataTable();

            DataRow dr = null;
            foreach (OncologyQuestionBuilder OQB in GetQuestionsDatabySection)
            {
                dt.Columns.Add(new DataColumn(OQB.QuestionText, typeof(string)));
            }

            var getContols = GetQuestionsDatabySection;

            for (int i = 1; i <= 1; i++)
            {
                dr = dt.NewRow();
                foreach (OncologyQuestionBuilder OQB in getContols)
                {

                    dr[OQB.QuestionText] = OQB.DynamicAttributeId;

                }
                dt.Rows.Add(dr);
            }

            //Store the DataTable in ViewState
            ViewState["CurrentTable"] = dt;
            gvQuestions.GridLines = GridLines.None;
            gvQuestions.ShowFooter = true;
            gvQuestions.DataSource = dt;
            gvQuestions.DataBind();
        }



        private void AddNewRowToGrid()
        {
            try
            {

                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    DataRow drCurrentRow = null;

                    if (dtCurrentTable.Rows.Count > 0)
                    {
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {
                            var GetQuestionsData = new OncologyInformation().SelectOncologyQuestions(Request.QueryString["ProjectID"].ToString(), "1");
                            var GetQuestionsDatabySection = from qd in GetQuestionsData where qd.SectionName == "Test B" select qd;

                            drCurrentRow = dtCurrentTable.NewRow();
                            foreach (OncologyQuestionBuilder OQB in GetQuestionsDatabySection)
                            {
                                drCurrentRow[OQB.QuestionText] = OQB.DynamicAttributeId;
                            }
                        }

                        //add new row to DataTable
                        dtCurrentTable.Rows.Add(drCurrentRow);

                        //Store the current data to ViewState
                        ViewState["CurrentTable"] = dtCurrentTable;

                        //Rebind the Grid with the current data
                        gvQuestions.DataSource = dtCurrentTable;
                        gvQuestions.DataBind();
                    }
                }
                else
                {
                    Response.Write("ViewState is null");
                }
            }
            catch (Exception e)
            {
            }


            //Set Previous Data on Postbacks
            SetPreviousData();
        }


        private void SetPreviousData()
        {
            try
            {
                int rowIndex = 0;
                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dt = (DataTable)ViewState["CurrentTable"];
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            //extract the TextBox values
                            var box1 = gvQuestions.Rows[rowIndex].Cells[0].Text;



                            //box1.Text = dt.Rows[i]["Column1"].ToString();
                            //box2.Text = dt.Rows[i]["Column2"].ToString();
                            //box3.Text = dt.Rows[i]["Column3"].ToString();

                            rowIndex++;

                        }
                    }

                }
            }
            catch (Exception e)
            {

            }

        }



        protected void ButtonAdd_Click1(object sender, EventArgs e)
        {
            AddNewRowToGrid();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvQuestions.Rows.Count; i++)
            {
                //extract the TextBox values
                TextBox box1 = (TextBox)gvQuestions.Rows[i].Cells[1].FindControl("email");
                TextBox box2 = (TextBox)gvQuestions.Rows[i].Cells[2].FindControl("FirstName");
                TextBox box3 = (TextBox)gvQuestions.Rows[i].Cells[3].FindControl("LastName");

                if (box1.Text != null && box2 != null && box3 != null)
                {
                    .....

                }
            }

            Response.Redirect("invitation-selection.aspx?env=" + Session["EventType"].ToString() + "&evID=" + Session["EventID"].ToString(), true);
        }

        protected void gvQuestions_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Button btn = new Button();
                btn.Text = "Add Row ";
                btn.CssClass = "addBtn";
                btn.Click += new EventHandler(ButtonAdd_Click1);

                e.Row.Cells[0].Controls.Add(btn);


            }
        }

 protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell tc in e.Row.Cells)
                {
                    tc.CssClass = "casetableheaderstudyList";
                }
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {


                var cellCount = e.Row.Cells.Count;

                if (cellCount >= 1)
                {
                    var ControlType = e.Row.Cells[0].Text;
                    var DataTypeId = e.Row.Cells[0].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "0";
                    e.Row.Cells[0].Controls.Add(getControl(DataTypeId, rowCellID));

                }
                if (cellCount >= 2)
                {
                    var ControlType = e.Row.Cells[1].Text;
                    var DataTypeId = e.Row.Cells[1].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "1";
                    e.Row.Cells[1].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 3)
                {
                    var ControlType = e.Row.Cells[2].Text;
                    var DataTypeId = e.Row.Cells[2].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "2";
                    e.Row.Cells[2].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 4)
                {
                    var ControlType = e.Row.Cells[3].Text;
                    var DataTypeId = e.Row.Cells[3].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "3";
                    e.Row.Cells[3].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 5)
                {
                    var ControlType = e.Row.Cells[4].Text;
                    var DataTypeId = e.Row.Cells[4].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "4";
                    e.Row.Cells[4].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 6)
                {
                    var ControlType = e.Row.Cells[5].Text;
                    var DataTypeId = e.Row.Cells[5].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "5";
                    e.Row.Cells[5].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 7)
                {
                    var ControlType = e.Row.Cells[6].Text;
                    var DataTypeId = e.Row.Cells[6].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "6";
                    e.Row.Cells[6].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 8)
                {
                    var ControlType = e.Row.Cells[7].Text;
                    var DataTypeId = e.Row.Cells[7].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "7";
                    e.Row.Cells[7].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 9)
                {
                    var ControlType = e.Row.Cells[8].Text;
                    var DataTypeId = e.Row.Cells[8].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "8";
                    e.Row.Cells[8].Controls.Add(getControl(DataTypeId, rowCellID));
                }
                if (cellCount >= 10)
                {
                    var ControlType = e.Row.Cells[9].Text;
                    var DataTypeId = e.Row.Cells[9].Text;
                    string rowCellID = e.Row.RowIndex.ToString() + "9";
                    e.Row.Cells[9].Controls.Add(getControl(DataTypeId, rowCellID));
                }





                foreach (TableCell tc in e.Row.Cells)
                {
                    tc.BorderStyle = BorderStyle.None;
                    tc.BorderWidth = 0;
                    //tc.BorderColor = System.Drawing.Color.Transparent;
                    tc.CssClass = "casetableColumsStudyList";
                }
            }
        }

0 个答案:

没有答案