Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 14th, 2006, 10:39 AM
Registered User
 
Join Date: Jan 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically generated textboxes and their values

Hi,

I have some Textbox controls on my page that are generated by a for-loop for each tuple from a database. Their IDs are textboxX where X is the counter from the for-loop.

Now, I activated postback for the textboxes, so that when the user changes the value in one of them, the page does a postback.

To handle the values from the postback, I test Page.IsPostBack. If this test is true, I want to use the new value (from the textbox that did the postback) to ie update the database. So far, I just loop through all the textboxes and update all the values (since I can't find out which control did the postback).

Code:
if (Page.IsPostBack) 
{
    for (int i = 0; i < nAmountOfTextBoxes; i++)
    {
        int value;   // value from textbox
        try
        {
            value = int.Parse(((TextBox)myPanel.FindControl("textbox" + i.ToString())).Text);
        }
        catch
        {
            // Couldn't parse
            value = -1;
        }

        if (value >= 0)
        {
            deportToSiberia(value);// do something with the value
        }
    } // end for
}
This doesn't work. I didn't really expect it to, but I don't know any other way to go about with it. How can I fetch the values from a postback in my code? Any help is greatly appreciated.

Thanks,
Frode
 
Old April 17th, 2006, 03:46 PM
Registered User
 
Join Date: Jan 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Found a solution. In case anyone stumbles upon this thread wondering the same thing, here it is (in short):

In the Page_Init I added all the textboxes. I checked !Page.IsPostBack each time to see if the values from the database should be loaded.

In the Page_Load I looped through each control in the panel with
Code:
if(Page.IsPostBack) 
{
    foreach( Control c in myPanel.Controls )
    {
        if( c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") )
        {
            TextBox t = (TextBox)c;
            deportToSiberia(t.Text); // Do something with the value from the postback
        }

    } // end for
} // end postback check
This article helped me a lot http://aspnet.4guysfromrolla.com/articles/092904-1.aspx






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically generated dropdownlist csun ASP.NET 1.0 and 1.1 Basics 3 April 8th, 2008 07:38 AM
dynamically generated form SaraJaneQ Javascript How-To 4 November 9th, 2006 11:11 AM
automatically generated textboxes noor ASP.NET 1.0 and 1.1 Basics 6 May 11th, 2005 09:28 AM
dynamically generated submenus elladi Dreamweaver (all versions) 9 December 24th, 2004 08:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.