473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to Persist an added Control in the form.

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

Aug 12 '08 #1
5 1410
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" <no*****@chello.nlwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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);
}
}

Aug 13 '08 #2

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote
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.
Aug 13 '08 #3
That's fine, but you don't need to add gv to Controls if you have it defined
in the markup.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Marc" <m.********@removethischello.nlwrote in message
news:Oa*************@TK2MSFTNGP06.phx.gbl...
>
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote
>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.

Aug 13 '08 #4

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote
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.
Aug 13 '08 #5
This line of yours:
gv.DataSource = employee

takes care about linking the gridview to the table. And the next line
gv.DataBind(); actually populates the grid. In ASP.NET language it is called
databinding.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Marc" <m.********@removethischello.nlwrote in message
news:uk**************@TK2MSFTNGP05.phx.gbl...
>
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote
>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.

Aug 13 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sam | last post by:
I have a custom control (MyTextBox - taken from Microsoft website) that implements the IPostBackDataHandler interface. It is added to the controls collection of a placeholder control during the...
2
by: Devin Fensterheim | last post by:
Does anyone know of an issue with a control's viewstate not persisting when dynamically adding custom controls to a web form using AddAt? If a control is added to an HTML form directly using the...
6
by: Henri | last post by:
Very strange problem : if I write: <asp:TextBox runat="server" id="myBox" /> the control's ViewState stays always empty, so it loses its properties if it's not always displayed. But if I...
0
by: Umut Tezduyar | last post by:
I have build a sample control but the designer doesn't communicate it to parse the childen. Please if you are an control developer, spend your 10 minutes to solve my problem. The main problem...
0
by: Jeremy Chapman | last post by:
I have included below virtually all the code to a control I'm trying to build. My issue is that an array list property in my control does not get persisted properly to the aspx page code in design...
10
by: Carlos | last post by:
Hi all, I have a form with an input radio control in a template field. When the user selects an option, and press a button the selection disappears.. I would like the selection to persist after...
6
by: Skysurfer | last post by:
Is it possible to know when a control is added to a form in Design time? I tryed with ControlAdded, but this is also fired when the program starts. I want to set some properties only when a...
0
NeoPa
by: NeoPa | last post by:
Introduction: We get fairly frequent questions on here about why settings (including both values AND formatting) of unbound controls on a form, are not stored for reference later, but instead, each...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.