Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Viewstate lost data after postback

$
0
0

Hi

I am dynamically creating textboxes inside gridview.

I am clicking addnew row button , a new row is added,if i add another row ,the last row(2nd row disappears ) after postback.the viewstate contains only first row values and only ID of second row and other text values of 2nd row are disappears

for exampele

If i enter 2 rows

ID Column1  Column2 Column3

1  xxxx            aaaa         bbbb

2 mmm          pppp      ccccc

The view state after postback contains

ID Column1 Column2 Column3

1 xxxx aaaa bbbb

Please help me to fix the issue .If there are 3 rows entered in gridview,the viewstate contails only 2 rows after postback.I have given the code below

protected void Saving(object sender, EventArgs e)
        {

            StringCollection sc = new StringCollection();

            DataTable dt = new DataTable();
            DataRow dr = null;
            SqlConnection conn = new SqlConnection(GetConnectionString());
            conn.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter("select * from SampleTable order by ID", conn);
            sqlDA.Fill(dt);//This comes from database


            if (ViewState["CurrentTable"] != null)//Last row values are missing here only ID is there.
            {


                dtCurrentTable = (DataTable)ViewState["CurrentTable"];

                if (dtCurrentTable.Rows.Count > 0)
                {

                    var qry1 = dtCurrentTable.AsEnumerable().Select(a => new { ID = a["ID"].ToString() });
                    var qry2 = dt.AsEnumerable().Select(b => new { ID = b["ID"].ToString() });

                    var exceptAB = qry1.Except(qry2);

                    DataTable dtMisMatch = (from a in dtCurrentTable.AsEnumerable() join ab in exceptAB on a["ID"].ToString() equals ab.ID select a).CopyToDataTable();
                    for (int i = 0; i < dtMisMatch.Rows.Count; i++)
                    {
                        int Id = Int32.Parse(dtMisMatch.Rows[i]["ID"].ToString());
                        string Col1 = dtMisMatch.Rows[i]["Column1"].ToString();
                        string Col2 = dtMisMatch.Rows[i]["Column2"].ToString();
                        string Col3 = dtMisMatch.Rows[i]["Column3"].ToString();
                        InsertData(Id, Col1, Col2, Col3);

                    }
                }

            }

        }


    private void AddNewRowToGrid()
        {

            

            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = ((DataTable)ViewState["CurrentTable"]);//before adding new row
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        TextBox boxID = (TextBox)Gridview1.Rows[rowIndex].Cells[0].FindControl("txtID");///the Id value is not incremented
                        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[3].FindControl("TextBox3");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["ID"] = Int64.Parse(boxID.Text) + 1; //identity + 1;
                        dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Column3"] = 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();
        }
Thanks and Regards
N.Ram




Viewing all articles
Browse latest Browse all 23244

Trending Articles