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