473,761 Members | 5,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(objec t sender, System.EventArg s e)
{
if (!Page.IsPostBa ck)
{
counter = 0;
ViewState["counter"] = counter;
}
counter = (int) ViewState["counter"];

for (int i = 0; i != counter; i++)
{
TextBox tempotext = new TextBox();
tempotext.Enabl eViewState = false;
Panel1.Controls .Add(tempotext) ;
}
(counttextboxes is an protected int declared in the class)

Button1_Click looks:

private void Button1_Click(o bject sender, System.EventArg s e)
{

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

}

Button2_Click is:

private void Button2_Click(o bject sender, System.EventArg s 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 1552
On Fri, 9 Sep 2005 08:13:03 -0700, "the friendly display name"
<th************ ********@discus sions.microsoft .com> wrote:

private void Page_Load(objec t sender, System.EventArg s e)
{
if (!Page.IsPostBa ck)
{
counter = 0;
ViewState["counter"] = counter; PopulateControl s(); }

}

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

(counttextboxe s is an protected int declared in the class)

Button1_Clic k looks:

private void Button1_Click(o bject sender, System.EventArg s e)
{

counter = (int) ViewState["counter"];
counter++;
ViewState["counter"] = counter; PopulateControl s();
}

Button2_Clic k is:

private void Button2_Click(o bject sender, System.EventArg s e)
{
counter = (int) ViewState["counter"];
counter--;
ViewState["counter"] = counter; PopulateControl s();
}

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************ ********@discus sions.microsoft .com> wrote:

private void Page_Load(objec t sender, System.EventArg s e)
{
if (!Page.IsPostBa ck)
{
counter = 0;
ViewState["counter"] = counter;

PopulateControl s();
}


}

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

(counttextboxe s is an protected int declared in the class)

Button1_Clic k looks:

private void Button1_Click(o bject sender, System.EventArg s e)
{

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

PopulateControl s();

}

Button2_Clic k is:

private void Button2_Click(o bject sender, System.EventArg s e)
{
counter = (int) ViewState["counter"];
counter--;
ViewState["counter"] = counter;

PopulateControl s();

}

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
1833
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. I want to dim various textboxes with different names depending on what I retrieve from SQL. I tried the following: dim dtrcondet.item("contact_detail_description") as new textbox() but this does not work
2
16247
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. However, when i submit the page, i am unable to read the values entered in these new textboxes. I have first tried to use System.Web.UI.WebControls.TextBox namespace, however, i was unable to read any value from my textboxes using the following...
4
1908
by: Bhavin | last post by:
How can I capture user entered values from dynamically created controls in ASP.NET ? Thanks,
1
1962
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 these text boxes when the user submits or posts back to the server. My intention was to create a textboxvalidator function which takes the control name as argument and returns an true if
1
1274
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. Also in the form is a datagrid that has some custom validation performed on values within its rows. The validation is server side but not tied into any ValidatorControls. When the form is submitted, if the datagrid contains values that are invalid...
0
5293
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 the days in the week with dates and textboxes to fill in some data. row 1: question
4
51670
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 controls need to be dynamically loaded according to user input. This simple example can be further extended to dynamically load custom web user controls. Background Please familiarize yourself with the ASP.NET Page Life Cycle. It is crucial to...
2
5034
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 is this, I have a page which uses multiple postbacks to generate a list of dynamic text boxes with appropriate labels. However, when I do the final postback to enter the values in the dynamic textboxes into the database the values seem to become...
4
4762
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 to demonstrate my issues. Lets say I have a WebForm with a DropDownList where the user selects a number from 1 to 10 (the DropDownList is not dynamically created). I also have a button on there that I use to trigger a PostBack. Based on the...
2
1400
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() End If Dim ds As New DataSet Dim name, unit ,namunt As String da.Fill(ds, "sys_params")
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.