saving a value in page_init for use later in postback

P

paulcis

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

}
}
}
 
B

bruce barker

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)


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

}
}
}
 
P

paulcis

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



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


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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top