473,396 Members | 1,996 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,396 software developers and data experts.

saving a value in page_init for use later in postback


I am trying to produce a dynamic form from a database with unknown
amount of records.
I need to read the values and create a new textbox for each.
I need to create the textboxes at page_init stage as indicated by
microsoft.
But I can't seem to save the number of controls in a viewstate
variable or a session variable
when creating them in page_init .

see code below


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;

/* microsoft says that you create the controls on page init
* then in page load you set the properties so view state will be
saved
* then on postback you have to recreate the controls again to view
posted back possibly edited values
* from text box controls
*
*
*
*
*/


public partial class _Default : System.Web.UI.Page
{

TableRow r;
TableCell c1;

void Page_Init()
{
if (!IsPostBack)
{

int x = 1;

this.NumberOfControls = 17;

while (x < this.NumberOfControls)
{
r = new TableRow();
c1 = new TableCell();

TextBox t = new TextBox();

c1.Controls.Add(t);

r.Cells.Add(c1);

Table1.Rows.Add(r);
t.ID = "textbox" + x.ToString(); // you must add
control before setting ID ?

Response.Write(t.ID);

x++;

} //end while
} //end if
} //end func


protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }

}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int count = this.NumberOfControls;


for (int i = 1; i < count; i++)
{
string Boxname = "textbox" + i.ToString();
TextBox t;
t = Page.FindControl(Boxname) as TextBox;

t.Text = "textbox" + i.ToString();

}
}
else
{
// after Postback

Response.Write("in postback <BR>");
int count = this.NumberOfControls;


count = 17;

for (int i = 1; i < count; i++)
{
TextBox tx = new TextBox();

tx.ID = "textbox" + i.ToString();

tx.Visible = false;

Page.Form.Controls.Add(tx);
}
}
}


protected void Button1_Click(object sender, EventArgs e)
{

int count = this.NumberOfControls;
for (int i = 1; i < count; i++)
{
string Boxname = "textbox" +
i.ToString();
TextBox s;
s = Page.FindControl(Boxname) as TextBox;
Response.Write(s.Text + " TBox" + "<BR>");

}
}
}

May 10 '07 #1
2 3992
remove the if statement in the oninit. also you need to add the table to
the page.

a better approach is to have oninit only create controls on postback,
and onload only create controls if not postback.

even if a button click will change the number of controls, you want to
recreate the old batch in oninit to get the postback values, then in the
click, you delete the controls and create a new set.
-- bruce (sqlwork.com)

paulcis wrote:
I am trying to produce a dynamic form from a database with unknown
amount of records.
I need to read the values and create a new textbox for each.
I need to create the textboxes at page_init stage as indicated by
microsoft.
But I can't seem to save the number of controls in a viewstate
variable or a session variable
when creating them in page_init .

see code below


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;

/* microsoft says that you create the controls on page init
* then in page load you set the properties so view state will be
saved
* then on postback you have to recreate the controls again to view
posted back possibly edited values
* from text box controls
*
*
*
*
*/


public partial class _Default : System.Web.UI.Page
{

TableRow r;
TableCell c1;

void Page_Init()
{
if (!IsPostBack)
{

int x = 1;

this.NumberOfControls = 17;

while (x < this.NumberOfControls)
{
r = new TableRow();
c1 = new TableCell();

TextBox t = new TextBox();

c1.Controls.Add(t);

r.Cells.Add(c1);

Table1.Rows.Add(r);
t.ID = "textbox" + x.ToString(); // you must add
control before setting ID ?

Response.Write(t.ID);

x++;

} //end while
} //end if
} //end func


protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }

}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int count = this.NumberOfControls;


for (int i = 1; i < count; i++)
{
string Boxname = "textbox" + i.ToString();
TextBox t;
t = Page.FindControl(Boxname) as TextBox;

t.Text = "textbox" + i.ToString();

}
}
else
{
// after Postback

Response.Write("in postback <BR>");
int count = this.NumberOfControls;


count = 17;

for (int i = 1; i < count; i++)
{
TextBox tx = new TextBox();

tx.ID = "textbox" + i.ToString();

tx.Visible = false;

Page.Form.Controls.Add(tx);
}
}
}


protected void Button1_Click(object sender, EventArgs e)
{

int count = this.NumberOfControls;
for (int i = 1; i < count; i++)
{
string Boxname = "textbox" +
i.ToString();
TextBox s;
s = Page.FindControl(Boxname) as TextBox;
Response.Write(s.Text + " TBox" + "<BR>");

}
}
}
May 10 '07 #2
On 10 May, 16:56, bruce barker <nos...@nospam.comwrote:
remove the if statement in the oninit. also you need to add the table to
the page.

a better approach is to have oninit only create controls on postback,
and onload only create controls if not postback.

even if a button click will change the number of controls, you want to
recreate the old batch in oninit to get the postback values, then in the
click, you delete the controls and create a new set.

-- bruce (sqlwork.com)

paulcis wrote:
I am trying to produce a dynamic form from a database with unknown
amount of records.
I need to read the values and create a new textbox for each.
I need to create the textboxes at page_init stage as indicated by
microsoft.
But I can't seem to save the number of controls in a viewstate
variable or a session variable
when creating them in page_init .
see code below
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;
/* microsoft says that you create the controls on page init
* then in page load you set the properties so view state will be
saved
* then on postback you have to recreate the controls again to view
posted back possibly edited values
* from text box controls
*
*
*
*
*/
public partial class _Default : System.Web.UI.Page
{
TableRow r;
TableCell c1;
void Page_Init()
{
if (!IsPostBack)
{
int x = 1;
this.NumberOfControls = 17;
while (x < this.NumberOfControls)
{
r = new TableRow();
c1 = new TableCell();
TextBox t = new TextBox();
c1.Controls.Add(t);
r.Cells.Add(c1);
Table1.Rows.Add(r);
t.ID = "textbox" + x.ToString(); // you must add
control before setting ID ?
Response.Write(t.ID);
x++;
} //end while
} //end if
} //end func
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int count = this.NumberOfControls;
for (int i = 1; i < count; i++)
{
string Boxname = "textbox" + i.ToString();
TextBox t;
t = Page.FindControl(Boxname) as TextBox;
t.Text = "textbox" + i.ToString();
}
}
else
{
// after Postback
Response.Write("in postback <BR>");
int count = this.NumberOfControls;
count = 17;
for (int i = 1; i < count; i++)
{
TextBox tx = new TextBox();
tx.ID = "textbox" + i.ToString();
tx.Visible = false;
Page.Form.Controls.Add(tx);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int count = this.NumberOfControls;
for (int i = 1; i < count; i++)
{
string Boxname = "textbox" +
i.ToString();
TextBox s;
s = Page.FindControl(Boxname) as TextBox;
Response.Write(s.Text + " TBox" + "<BR>");
}
}
}
Many thanks bruce
I will remove the if statement and try adding the table to the page
I do not fully understand your second suggestion yet
..but I will try and read up a bit more on page lifecycle

May 11 '07 #3

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

Similar topics

9
by: wASP | last post by:
Hello again to all of you geniuses, I'm having a problem trying to load dynamic controls at the initialization phase. I've read the docs, and I thought I had it figured out:...
2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
4
by: jack | last post by:
Hello, How should I use Page_init and Page_load with making ASP.NET Apps? Thanks, Jack
2
by: HoustonFreeways | last post by:
Suppose I have a datagrid control which defines the showfooter property <dg:datagridconfirm id="dg_inbox" Runat="server" showfooter=false> During subsequent postbacks of the page, I change the...
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
6
by: ad | last post by:
Hi, What is the difference between Page_init and Page_Load event?
5
by: tshad | last post by:
I have a PageInit.ascx that I want to put in all my pages and have it execute only once during the "not IsPostback" section. I also need it to execute first before anything else. I have it set...
1
by: 1388-2/HB | last post by:
I learned that if you want asp.net to maintain viewstate of a dynamic control, you instantiate / add it to the html form during the page_init phase. At least that's the way I've been coding my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.