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

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

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
Nov 15 '05 #1
4 1574
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" <jm*****@fnal.gov> wrote in message
news:C2**********************************@microsof t.com...
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

Nov 15 '05 #2
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

Nov 15 '05 #3
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" <jm*****@fnal.gov> wrote in message
news:C2**********************************@microsof t.com...
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

Nov 15 '05 #4
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

Nov 15 '05 #5

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

Similar topics

7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
1
by: Edlueze | last post by:
I have a number of forms with large textboxes for the purposes of editing memo fields, and just recently I've noticed some bizarre behavior. For some textboxes the END and HOME keys do nothing,...
11
by: ian.davies52 | last post by:
Is there anything I can do about the apparent limit on the number of textboxes that have calculations as their control source on a form or report in ms-access? I have a query that pulls together...
2
by: JC | last post by:
Hi, I am a novice at .net and have created a page of text boxes that are populated from a database. I want the user to be able to change the values in the box then submit the page and have these...
2
by: Anand Sagar | last post by:
I have a Panel1 and button1 on my webform. At runtime, I create 2 textboxes. I do it at the Page_Load event. I put the code within the " If Not isPostBack" For the button click event, I will do...
2
by: the friendly display name | last post by:
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...
5
by: \A_Michigan_User\ | last post by:
I'm using asp.net/vb.net/ado.net to create a very simple user interface. (Everything works if I use STATICALLY created textBoxes... but not when I make them DYNAMICALLY.) 1. Execute a SQL...
4
by: rseanbarnes | last post by:
Hello, I am designing a program using Visual Basic 2005 Express Edition that will ask the user for a number between 1 and 50. When the button on the form is clicked, another form (Form2) opens...
2
by: Jonathan Boivin | last post by:
Hi people, Let me introduce to how I get this error. I have a form which load all my bills representation depending upon filters which each bill is a usercontrol of my own having some...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.