Dynamic Textboxes

G

Guest

Hello newsgroup..

Following problem:

I have two buttons on a page, If I click on button1, an additional textbox
should be dynamical created and added to a panel. If I click on button2, one
box should be removed from the Panel.

The values of the textboxes should of course be the same through all
viewstates.

My main problem is follwowing:

the creation of the textboxes is happening under Page_Load

This is how it looks:

private void Page_Load(object sender, System.EventArgs e)
{


if (!Page.IsPostBack)
{
counter = 0;
ViewState["counter"] = counter;
}


counter = (int) ViewState["counter"];



for (int i = 0; i != counter; i++)
{
TextBox tempotext = new TextBox();
tempotext.EnableViewState = false;
Panel1.Controls.Add(tempotext);
}


(counttextboxes is an protected int declared in the class)

Button1_Click looks:

private void Button1_Click(object sender, System.EventArgs e)
{

counter = (int) ViewState["counter"];
counter++;
ViewState["counter"] = counter;



}

Button2_Click is:

private void Button2_Click(object sender, System.EventArgs e)
{
counter = (int) ViewState["counter"];
counter--;
ViewState["counter"] = counter;

}


The main problem is now, that counttextboxes in Page_Load is always a number
smaller than in Button1_Click etc. This has to do with the fact, that
Page_Load is of course always processed before button1_click, even after a
click.

That leads to the problem, that on the first click on button1, no textbox
appears, and on the second click, only one box appears, on the third click,
only two boxes are there instead of the correct three...


I've tried to move the textbox generation code to function, that is wired
with the Pre_Render event, the textboxes appear now in their correct
quantity, but their values always vanish with each Postback (doesn't happen,
if the logic is in page_load). I tried to save the textboxes value in the
ViewState (if using the pre_Render method) , doesn't work. The value
TextBox.Text is empty.

Any ideas?
 
T

Tom John

private void Page_Load(object sender, System.EventArgs e)
{


if (!Page.IsPostBack)
{
counter = 0;
ViewState["counter"] = counter; PopulateControls();
}
}

private void PopulateControls()
{
counter = (int) ViewState["counter"];
for (int i = 0; i != counter; i++)
{
TextBox tempotext = new TextBox();
tempotext.EnableViewState = false;
Panel1.Controls.Add(tempotext);
}
}

(counttextboxes is an protected int declared in the class)

Button1_Click looks:

private void Button1_Click(object sender, System.EventArgs e)
{

counter = (int) ViewState["counter"];
counter++;
ViewState["counter"] = counter; PopulateControls();



}

Button2_Click is:

private void Button2_Click(object sender, System.EventArgs e)
{
counter = (int) ViewState["counter"];
counter--;
ViewState["counter"] = counter; PopulateControls();

}


Hope this helps

Tom
 
G

Guest

Tried something like this before..

The textboxes will lose their value with each postback.

Tom John said:
private void Page_Load(object sender, System.EventArgs e)
{


if (!Page.IsPostBack)
{
counter = 0;
ViewState["counter"] = counter; PopulateControls();
}
}

private void PopulateControls()
{
counter = (int) ViewState["counter"];
for (int i = 0; i != counter; i++)
{
TextBox tempotext = new TextBox();
tempotext.EnableViewState = false;
Panel1.Controls.Add(tempotext);
}
}

(counttextboxes is an protected int declared in the class)

Button1_Click looks:

private void Button1_Click(object sender, System.EventArgs e)
{

counter = (int) ViewState["counter"];
counter++;
ViewState["counter"] = counter; PopulateControls();



}

Button2_Click is:

private void Button2_Click(object sender, System.EventArgs e)
{
counter = (int) ViewState["counter"];
counter--;
ViewState["counter"] = counter; PopulateControls();

}


Hope this helps

Tom
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top