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"
}
}