Creating Textboxes on the fly - new ones appear but the old ones do not

G

Guest

Hi,

Im very new to c#, so forgive me if this is a really stupid question.
Im trying to create a form for entering purchase requests.
For each line item I have a quantity, a description unit cost and total cost.. all textboxes.
Next to this I have a button that will add a new set of textboxes so people can have more than one line item.
As it is now, when you push the button the textboxes appear, and everything below them is moved down accordingly, just as it should.
However, if you push the button AGAIN, the new textboxes are created, everything gets pushed down, but the previous line of created textboxes are gone.

this is how I am doing it now:
private void btnMore_Click(object sender, System.EventArgs e)
{
// local variables for base position of objects that will move
int budgetbase = 672;
int ctlbase = 672;
int chargesbase = 600;
int morebase = 456;
int clearreqbase = 848;
int textboxbase = 456;
lineitems = Convert.ToInt32(ViewState["counter"]);
lineitems++;
ViewState["counter"] = lineitems;

// increment lineitems and calculate offset

offset = lineitems * 30;

Control frm = FindControl("Form1");

txtStatus.Text = lineitems.ToString();
// create a new quantity textbox
tb = new TextBox();
tb.ID = "Quantity"+ViewState["counter"].ToString();
tb.Style["Position"]="Absolute";
tb.Style["Left"] = "56px";
tb.Style["Top"] = System.Convert.ToString(textboxbase + offset) + "px";
tb.Width=64;
tb.Height=24;
frm.Controls.Add(tb);
tb = new TextBox();
// create a new item description textbox
tb.ID = "ItemDescription"+lineitems.ToString();
tb.Style["Position"]="Absolute";
tb.Style["Left"] = "144px";
tb.Style["Top"] = System.Convert.ToString(textboxbase + offset) + "px";
tb.Width=608;
tb.Height=24;
frm.Controls.Add(tb);

etc...

// move panels down 30 px
pnlBudget.Style["Top"] = System.Convert.ToString(budgetbase + offset) + "px";
pnlCharges.Style["Top"] = System.Convert.ToString(chargesbase + offset) + "px";
pnlCTI.Style["Top"] = System.Convert.ToString(ctlbase + offset) + "px";

//move buttons
btnMore.Style["Top"]= System.Convert.ToString(morebase + offset) + "px";
btnPurchasReq.Style["Top"]= System.Convert.ToString(clearreqbase + offset) + "px";
btnClear.Style["Top"]= System.Convert.ToString(clearreqbase + offset) + "px";
}

I have also tried setting up an array of textboxes, but I got the same result, so I have a feeling there is something else that my feeble newbie mind is not grasping here.
Thanks for any advice, and I appologize for the long post.

thanks

Jason
 
K

Kirk Graves

The problem is a common State issue with web apps. When you create the
objects on the server, they generate the HTML to render to the client. On
the round trip, when you are creating the next set of controls, the first
set no longer exist (remember you created them in code, not in the aspx
file, so they don't stick around in a stateless environment).

I think you will find that if you do any other postbacks on the page, the
controls will disappear then as well.

Your fix is actually fairly easy....
Remember the total number of lines you have created for this user, then
recreate all previously created rows (just set up a loop for the code you
already have written)

Kirk Graves
KRGIT Software

Jason M said:
Hi,

Im very new to c#, so forgive me if this is a really stupid question.
Im trying to create a form for entering purchase requests.
For each line item I have a quantity, a description unit cost and total cost.. all textboxes.
Next to this I have a button that will add a new set of textboxes so
people can have more than one line item.
As it is now, when you push the button the textboxes appear, and
everything below them is moved down accordingly, just as it should.
However, if you push the button AGAIN, the new textboxes are created,
everything gets pushed down, but the previous line of created textboxes are
gone.
this is how I am doing it now:
private void btnMore_Click(object sender, System.EventArgs e)
{
// local variables for base position of objects that will move
int budgetbase = 672;
int ctlbase = 672;
int chargesbase = 600;
int morebase = 456;
int clearreqbase = 848;
int textboxbase = 456;
lineitems = Convert.ToInt32(ViewState["counter"]);
lineitems++;
ViewState["counter"] = lineitems;

// increment lineitems and calculate offset

offset = lineitems * 30;

Control frm = FindControl("Form1");

txtStatus.Text = lineitems.ToString();
// create a new quantity textbox
tb = new TextBox();
tb.ID = "Quantity"+ViewState["counter"].ToString();
tb.Style["Position"]="Absolute";
tb.Style["Left"] = "56px";
tb.Style["Top"] = System.Convert.ToString(textboxbase + offset) + "px";
tb.Width=64;
tb.Height=24;
frm.Controls.Add(tb);
tb = new TextBox();
// create a new item description textbox
tb.ID = "ItemDescription"+lineitems.ToString();
tb.Style["Position"]="Absolute";
tb.Style["Left"] = "144px";
tb.Style["Top"] = System.Convert.ToString(textboxbase + offset) + "px";
tb.Width=608;
tb.Height=24;
frm.Controls.Add(tb);

etc...

// move panels down 30 px
pnlBudget.Style["Top"] = System.Convert.ToString(budgetbase + offset) + "px";
pnlCharges.Style["Top"] = System.Convert.ToString(chargesbase + offset) + "px";
pnlCTI.Style["Top"] = System.Convert.ToString(ctlbase + offset) + "px";

//move buttons
btnMore.Style["Top"]= System.Convert.ToString(morebase + offset) + "px";
btnPurchasReq.Style["Top"]= System.Convert.ToString(clearreqbase + offset) + "px";
btnClear.Style["Top"]= System.Convert.ToString(clearreqbase + offset) + "px";
}

I have also tried setting up an array of textboxes, but I got the same
result, so I have a feeling there is something else that my feeble newbie
mind is not grasping here.
 
G

Guest

Hey, thanks very much for the reply
I don't know why I did'nt think of that solution. Im still pretty new to web programming

I imagine that if I recreate the old textboxes every time the button is pressed that any info entered into those fields will be lost
 
Top