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);
}
}
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);
}
}