using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace WebApplication1{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TextBox1.Text = ""; TextBox2.Text = ""; } protected void Button2_Click(object sender, EventArgs e) {// ADDED DATA TO THE TEXT BOX AND CLICKED ON ADD BUTTON AddNewData(); } protected void Button1_Click(object sender, EventArgs e) {// GOING TO ANOTHER FORM TO DISPLAY THE DATA Response.Redirect("Default.aspx"); } public void AddNewData() {// ADDING DATA AND STORING THE DATATABLE INTO THE SESSION DataTable dt = new DataTable(); if (Session["myDatatable"] != null) { dt = (DataTable)Session["myDatatable"]; } else { dt.Columns.Add("Name"); dt.Columns.Add("ID"); } DataRow drow = dt.NewRow(); drow["Name"] = TextBox2.Text.ToString(); drow["ID"] = TextBox1.Text.ToString(); dt.Rows.Add(drow); Session["myDatatable"] = dt; TextBox1.Text = ""; TextBox2.Text = ""; } }}
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace WebApplication1{ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindMyGridview(); } } protected void Button1_Click(object sender, EventArgs e) {// CLICKED ON THE BUTTON TO GO TO THE PAGE WITH TEXTBOXES TO ADD NEW DATA Response.Redirect("Webform1.aspx"); } public void BindMyGridview() {// BINDING THE Gridview1 FROM SESSION if (Session["myDatatable"] != null) { DataTable dt = (DataTable)Session["myDatatable"]; if ((dt != null) && (dt.Rows.Count > 0)) { GridView1.Visible = true; GridView1.DataSource = dt; GridView1.DataBind(); } else { GridView1.Visible = false; } } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {// Gridview1's RowEditing Event GridView1.EditIndex = e.NewEditIndex; BindMyGridview(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {// Gridview1's RowUpdating Event TextBox TextBoxWithID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2"); TextBox TextBoxWithName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4"); string NewID = TextBoxWithID.Text.ToString(); string NewName = TextBoxWithName.Text.ToString(); DataTable dt = (DataTable)Session["myDatatable"]; DataRow dr = dt.Rows[e.RowIndex]; dr["ID"] = NewID; dr["Name"] = NewName; dr.AcceptChanges(); Session["myDatatable"] = dt; GridView1.EditIndex = -1; BindMyGridview(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {// Gridview1's RowEditing Cancel Event GridView1.EditIndex = -1; BindMyGridview(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {// Gridview1's RowDeleting Event DataTable dt = (DataTable)Session["myDatatable"]; DataRow dr = dt.Rows[e.RowIndex]; dt.Rows.Remove(dr); GridView1.EditIndex = -1; BindMyGridview(); } }}