I have the following code:
Imports System.Data
Partial Class Students_AddWishes Inherits System.Web.UI.Page
Public dt As New DataTable
Protected S
You need to save the datatable to Session, because local variables are not preserved. So you need to do:
protected override OnLoad(..) //or OnInit
{
dt = Session["DataTable"] as DataTable;
if (dt == null)
{
dt = new DataTable();
//load columns
}
}
protected override OnUnLoad(..)
{
Session["DataTable"] = dt;
}
Session saves the data table reference across postbacks as the web is stateless.
Your page load event you are not checking if it is a post back:
If Not IsPostBack Then
'process code if it is not a post back
End If
Everytime you click the btnAdd button your page does a post back to the server.
I just noticed that you probably are not understanding the life time of an object.
You had done this in your code:
Public dt As New DataTable
The problem with that is you have defined this is a class variable and once the page has loaded you have an instance of type dt that may have some columns associated with it. But as soon as you record an event such as a button click that reference is destroyed and a new dt is created.
You will have to make some use of session variables or a database to store the state of dt.
Here is an example in C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
dt.Columns.Add("univirsity", System.Type.GetType("System.Int32"));
dt.Columns.Add("major", System.Type.GetType("System.Int32"));
Session["MyDataTable"] = dt;
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
DataRow row1 = t.NewRow();
row1["ID"] = t.Rows.Count + 1;
row1["univirsity"] = 3;
row1["major"] = 31;
t.Rows.Add(row1);
Session["MyDataTable"] = t;
GridView1.DataSource = t;
GridView1.DataBind();
}
And the same code in vb.net:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.Add("ID", System.Type.[GetType]("System.Int32"))
dt.Columns.Add("univirsity", System.Type.[GetType]("System.Int32"))
dt.Columns.Add("major", System.Type.[GetType]("System.Int32"))
Session("MyDataTable") = dt
End If
End Sub
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim t As DataTable = DirectCast(Session("MyDataTable"), DataTable)
Dim row1 As DataRow = t.NewRow()
row1("ID") = t.Rows.Count + 1
row1("univirsity") = 3
row1("major") = 31
t.Rows.Add(row1)
Session("MyDataTable") = t
GridView1.DataSource = t
GridView1.DataBind()
End Sub
So now what the code does is instantiate a new datatable object as long as we are on the page (first post back) and adds the columns. Once it has defined the data table we throw it in some session state. When you click the add button you cannot in your previous code just keep using dt because dt scope was lost in your prior code. We do this by assigning the sessioned datatable which was stored prior to a temp datatable. We add the row and reset the session that way the next time we add a row it will display the second row, then third row, and so on...
I recommend a good asp.net book on such as Beginning ASP.net 3.5 in C# 2008. There are a ton of vb.net books on the same subject matter.