473,407 Members | 2,598 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,407 software developers and data experts.

Dynamic Textboxes

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?
Nov 19 '05 #1
2 1517
On Fri, 9 Sep 2005 08:13:03 -0700, "the friendly display name"
<th********************@discussions.microsoft.co m> wrote:

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
Nov 19 '05 #2
Tried something like this before..

The textboxes will lose their value with each postback.

"Tom John" wrote:
On Fri, 9 Sep 2005 08:13:03 -0700, "the friendly display name"
<th********************@discussions.microsoft.co m> wrote:

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

Nov 19 '05 #3

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

Similar topics

7
by: Mark | last post by:
I want to create textboxes dynamically but with dynamic names also. i am retrieveing a load of values from a table in SQL into a DataReader. Then i want to create textboxes from those variables. ...
2
by: charliewest | last post by:
I need to create textboxes in real-time, the actual number of which is determine by a result from a database query. I have been able to create the controls, and then add them to the ASPX page....
4
by: Bhavin | last post by:
How can I capture user entered values from dynamically created controls in ASP.NET ? Thanks,
1
by: Kum | last post by:
Hi, I need help in asp.net dynamic textbox controls validation. I am creating textbox controls dynamically on a asp.net webpage. Now after creating the textboxes on the page I want to validate...
1
by: Elliot Rodriguez | last post by:
I have a dropdown list that defines the number of textboxes that appear within a particular panel. The boxes are created when the dropdownlist's SelectedIndexChanged event fires on postback. ...
0
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
2
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
4
by: =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post by:
I have a WebForm where I'm dynamically creating some controls and I'm having difficulty understanding how the state is being persisted and how to work with it. I've created a simplified example...
2
by: bharathi228 | last post by:
my code for retrieving values from database Dim da As New SqlDataAdapter("select parameter_name,parameter_units from sys_params", con) If con.State = ConnectionState.Closed Then con.Open()...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.