How to Persist an added Control in the form.

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

Ok, I am studying dot net, and I have a probably newbee sort of question
again. I have made a form and a button that fills the gridview on the form.
I also want to make a delete and edit button. But everytime I click the
delete or edit button, the gridview disappears again. I guess it is not
recreated after a page reload, but what can I do to fix this?

This is my code:

using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// ClientScript.RegisterStartupScript(GetType(), "MyAlert", "alert('page
load');", true);
// if (!IsPostBack)
{
gv.Style.Add("position", "absolute");
gv.Style.Add("left", "275px");
gv.Style.Add("top", "20px");
gv.AutoGenerateDeleteButton = true;
gv.RowDeleting += new GridViewDeleteEventHandler(gv_RowDeleting);
gv.AutoGenerateEditButton = true;
gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
}
}

protected void Button1_Click(object sender, EventArgs e)
{
//Create the DataTable named "employee"
DataTable employee = new DataTable("Employee");
DataColumn eid = new DataColumn("Eid");
eid.DataType = typeof(string);
eid.MaxLength = 10;
eid.Unique = true;
eid.AllowDBNull = false;
eid.Caption = "EID";
employee.Columns.Add(eid);
DataColumn firstName = new DataColumn("FirstName");
firstName.MaxLength = 35;
firstName.AllowDBNull = false;
employee.Columns.Add(firstName);
DataColumn lastName = new DataColumn("LastName");
lastName.AllowDBNull = false;
employee.Columns.Add(lastName);
DataRow newemployee1 = employee.NewRow();
newemployee1["Eid"] = "1";
newemployee1["FirstName"] = "Nancy";
newemployee1["LastName"] = "Davolio";
DataRow newemployee2 = employee.NewRow();
newemployee2["Eid"] = "2";
newemployee2["FirstName"] = "Nancy";
newemployee2["LastName"] = "Davolio";
employee.Rows.Add(newemployee1);
employee.Rows.Add(newemployee2);
form1.Controls.Add(gv);

// I tried this to make it persist but it does not seem to work.
form1.EnableViewState = true;

//get the table and display
gv.DataSource = employee;
gv.DataBind();
}
void gv_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "MyAlert",
"alert('Del');", true);
}
void gv_RowEditing(Object sender, GridViewEditEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "MyAlert",
"alert('Edit');", true);
}
}
 
What happens to your gridview that you need to call form1.Controls.Add(gv)?

ASP.NET is object-oriented. A standard way how you make a page is:
1) prepare your markup in either design or html view, whatever you like more
and
2) program events to implement actions.

With this, the code your write typically will manipulate with control
properties and do some data-related operations. Almost never in simple
scenarios you will need to create any controls dynamically.


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Marc said:
Ok, I am studying dot net, and I have a probably newbee sort of question
again. I have made a form and a button that fills the gridview on the
form. I also want to make a delete and edit button. But everytime I click
the delete or edit button, the gridview disappears again. I guess it is
not recreated after a page reload, but what can I do to fix this?

This is my code:

using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// ClientScript.RegisterStartupScript(GetType(), "MyAlert", "alert('page
load');", true);
// if (!IsPostBack)
{
gv.Style.Add("position", "absolute");
gv.Style.Add("left", "275px");
gv.Style.Add("top", "20px");
gv.AutoGenerateDeleteButton = true;
gv.RowDeleting += new GridViewDeleteEventHandler(gv_RowDeleting);
gv.AutoGenerateEditButton = true;
gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
}
}

protected void Button1_Click(object sender, EventArgs e)
{
//Create the DataTable named "employee"
DataTable employee = new DataTable("Employee");
DataColumn eid = new DataColumn("Eid");
eid.DataType = typeof(string);
eid.MaxLength = 10;
eid.Unique = true;
eid.AllowDBNull = false;
eid.Caption = "EID";
employee.Columns.Add(eid);
DataColumn firstName = new DataColumn("FirstName");
firstName.MaxLength = 35;
firstName.AllowDBNull = false;
employee.Columns.Add(firstName);
DataColumn lastName = new DataColumn("LastName");
lastName.AllowDBNull = false;
employee.Columns.Add(lastName);
DataRow newemployee1 = employee.NewRow();
newemployee1["Eid"] = "1";
newemployee1["FirstName"] = "Nancy";
newemployee1["LastName"] = "Davolio";
DataRow newemployee2 = employee.NewRow();
newemployee2["Eid"] = "2";
newemployee2["FirstName"] = "Nancy";
newemployee2["LastName"] = "Davolio";
employee.Rows.Add(newemployee1);
employee.Rows.Add(newemployee2);
form1.Controls.Add(gv);

// I tried this to make it persist but it does not seem to work.
form1.EnableViewState = true;

//get the table and display
gv.DataSource = employee;
gv.DataBind();
}
void gv_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "MyAlert",
"alert('Del');", true);
}
void gv_RowEditing(Object sender, GridViewEditEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "MyAlert",
"alert('Edit');", true);
}
}
 
Eliyahu Goldin said:
What happens to your gridview that you need to call
form1.Controls.Add(gv)?

I am studying ASP.NET, it's not real code. It's an assignment, not practical
code. The task practice is:

Practice 1 Create a new Web page and add a button to create a DataTable that
contains a schema for employees, a button that adds a DataRow into the
employees DataTable, a button that modifies an existing DataRow, and a
button that deletes a DataRow.
 
Eliyahu Goldin said:
That's fine, but you don't need to add gv to Controls if you have it
defined in the markup.

Yes you're right I am mistaken. Sorry, I am still learning. I actually tried
that, defining the GridView gv in the markup. But I add the Datatable with
the button code. And this link from the Datatable to the Gridview disappears
with every post back. It seems that is the problem.
 
Back
Top