0

I currently have a Gridview which is being created dynamically based on text input.

<asp:GridView ID="gvSession" runat="server" ShowFooter="false" CssClass="table table-striped table-bordered dataTable no-footer hover gvv" AutoGenerateColumns="true" ShowHeaderWhenEmpty="true">
      <Columns>
      </Columns>
    </asp:GridView> 

So, whenever my page have a PostBack, the controls such as TextBox and Label that is being created dynamically in the Gridview always disappear, thus the Gridview will be left with the number of rows created previously but is empty.

Is there a way to 'save' the information of the Gridview? Or am I just doing wrongly for my creation of Gridview

Here's how I created my Gridview.

ASPX.CS

protected void tbClassSession_TextChanged(object sender, EventArgs e)
        {
            GenerateRows();
        }

        private void SetInitialRow()
        {
            int numbers = int.Parse(this.tbClassSession.Text.Trim());
            DataTable dt = new DataTable();
            //you can add as many rows you want 
            dt.Columns.Add("Session No.", typeof(string));
            dt.Columns.Add("Lesson Date", typeof(DateTime));
            for (int i = 0; i < numbers; i++)
            {
                dt.Rows.Add("", null);
            }
            gvSession.DataSource = dt;
            gvSession.DataBind();

        }

        protected void GenerateRows()
        {
            this.SetInitialRow();
            int numbers = int.Parse(this.tbClassSession.Text.Trim());

                int cellCount = this.gvSession.Rows[0].Cells.Count;
                int rowsCount = this.gvSession.Rows.Count;
                foreach (GridViewRow row in this.gvSession.Rows)
                {
                    Label label = new Label();
                    label.Text = (row.RowIndex + 1).ToString();
                    label.Attributes.Add("runat", "server");
                    row.Cells[0].Controls.Add(label);
                }

                int rowCount = 0;

                foreach (GridViewRow row in this.gvSession.Rows)
                {
                    TextBox textBox = new TextBox();
                    textBox.TextMode = TextBoxMode.Date;

                    textBox.Text = "Hello World"
                }

        }
Alvin
  • 408
  • 3
  • 14
  • can you please add `gvSession.DataBind();` in last line of method `GenerateRows()` and let me know what happens ? – Ankit Nov 01 '16 at 15:02
  • Possible duplicate of [Dynamically Created Controls losing data after postback](http://stackoverflow.com/questions/17589268/dynamically-created-controls-losing-data-after-postback) – VDWWD Nov 01 '16 at 15:16
  • @AnkitkumarBhatt The `gvSession` is empty when i input into the `textbox` which triggers the creation of `Gridview` – Alvin Nov 01 '16 at 15:54
  • Here I can see you are calling `SetInitialRow()` in `GenerateRows()` so it will have something so you can add `gvSession.DataBind();` in the last line of `GenerateRows()`. – Ankit Nov 02 '16 at 06:10

0 Answers0