0

我有一个网格视图,用户必须在文本框中输入一些内容,并且用户类型将被插入到数据库中,我有这两个问题或问题,每次我点击“+”按钮(添加新闻行) , 上一行数据的文本框之一将被删除.. 并且选中的单选按钮也将被删除。这是截图问题是什么:

在此处输入图像描述

点击“+”按钮后,会变成这样:

在此处输入图像描述

这是我背后的代码:

 private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1");
        CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2");

        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++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");

                    drCurrentRow = dtCurrentTable.NewRow();


                    dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;       
                    dtCurrentTable.Rows[i - 1]["Hints"] = box3.Text;


                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GridView1.DataSource = dtCurrentTable;
                GridView1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

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

     private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("Question", typeof(string)));
        dt.Columns.Add(new DataColumn("Hints", typeof(string)));

        dr = dt.NewRow();
        dr["Question"] = string.Empty;
        dr["Hints"] = string.Empty;


        dt.Rows.Add(dr);


        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    private void SetPreviousData()
    {
        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++)
                {
                    TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");



                    //Setting previous text to the respective textboxes based on columns.
                    box1.Text = dt.Rows[i]["Question"].ToString();
                    box2.Text = dt.Rows[i]["Hints"].ToString(); 
                    box3.Text = dt.Rows[i]["Hints"].ToString(); 


                    Session["Question1"] = box1.Text;
                    rowIndex++;
                }
            }
        }
    }

当每列只有一个文本框时,我可以很容易地做到这一点..但现在每列有2个文本框...因为我只能根据列名将文本附加到文本框...另外,我如何维护感谢我的单选按钮帮助,谢谢!

4

1 回答 1

2
 private void AddNewRowToGrid()
        {
            int rowIndex = 0;

            CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1");
            CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2");

            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++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");

                        drCurrentRow = dtCurrentTable.NewRow();


                        dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;         
                        dtCurrentTable.Rows[i - 1]["Hints"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Hints1"] = box3.Text;


                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    GridView1.DataSource = dtCurrentTable;
                    GridView1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

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

         private void SetInitialRow()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("Question", typeof(string)));
            dt.Columns.Add(new DataColumn("Hints", typeof(string)));
            dt.Columns.Add(new DataColumn("Hints1", typeof(string)));

            dr = dt.NewRow();
            dr["Question"] = string.Empty;
            dr["Hints"] = string.Empty;
            dr["Hints1"] = string.Empty;


            dt.Rows.Add(dr);


            //Store the DataTable in ViewState
            ViewState["CurrentTable"] = dt;

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        private void SetPreviousData()
        {
            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++)
                    {
                        TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                        TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");



                        //Setting previous text to the respective textboxes based on columns.
                        box1.Text = dt.Rows[i]["Question"].ToString();
                        box2.Text = dt.Rows[i]["Hints"].ToString(); 
                        box3.Text = dt.Rows[i]["Hints1"].ToString(); 


                        Session["Question1"] = box1.Text;
                        rowIndex++;
                    }
                }
            }
        }

我刚刚添加了另一列,其余代码已经可以正常工作了。

于 2013-06-03T07:40:20.787 回答